<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="php,">










<meta name="description" content="php sqlite1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991">
<meta name="keywords" content="php">
<meta property="og:type" content="article">
<meta property="og:title" content="php 记录">
<meta property="og:url" content="http://yoursite.com/2019/05/13/php-记录/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="php sqlite1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-07-24T11:19:12.043Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="php 记录">
<meta name="twitter:description" content="php sqlite1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/05/13/php-记录/">



<meta name="referrer" content="never"> ​​​​


  <title>php 记录 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/13/php-记录/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">php 记录</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-13T10:34:43+08:00">
                2019-05-13
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  20.2k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  98 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="php-sqlite"><a href="#php-sqlite" class="headerlink" title="php sqlite"></a>php sqlite</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"># -- 查询当前数据库信息</span><br><span class="line">sqlite&gt; .database</span><br><span class="line"></span><br><span class="line"># -- 查询当前数据库下的所有表的信息</span><br><span class="line"># 可选参数 ? table name , 如果加上这个参数, 会按照输入的表面去显示, 支持 LIKE 方式查找</span><br><span class="line">sqlite&gt; .tables</span><br><span class="line">sqlite&gt; .tables %persion%</span><br><span class="line"></span><br><span class="line"># -- 创建一个表</span><br><span class="line">sqlite&gt; CREATE TABLE persions (</span><br><span class="line">        id INTEGER PRIMARY KEY AUTOINCREMENT,</span><br><span class="line">        name text,</span><br><span class="line">        age int);</span><br><span class="line"></span><br><span class="line"># -- 删除一个表</span><br><span class="line">sqlite&gt; drop table persions;</span><br><span class="line"></span><br><span class="line"># -- 修改表名</span><br><span class="line">sqlite&gt; ALTER TABLE persions RENAME TO persions2;</span><br><span class="line"># 查看修改结果</span><br><span class="line">sqlite&gt; .tables</span><br><span class="line"># 再改回去,不要影响后面的命令</span><br><span class="line">sqlite&gt; ALTER TABLE persions2 RENAME TO persions;</span><br><span class="line"></span><br><span class="line"># -- 查看表结构</span><br><span class="line">sqlite&gt; pragma table_info (<span class="string">'persions'</span>);</span><br><span class="line"></span><br><span class="line"># -- 修改字段, SQLite 不能像主流数据库那样直接修改字段, 只能添加一个新的字段</span><br><span class="line">#    如果必须要修改,可以先将表重命名一个其他的,再将数据插入即可</span><br><span class="line"># 添加新字段</span><br><span class="line">sqlite&gt; ALTER TABLE persions ADD COLUMN address text;</span><br><span class="line"></span><br><span class="line"># -- 添加数据, 规则和 MYSQL 很类似</span><br><span class="line"># 方式1</span><br><span class="line">sqlite&gt; INSERT INTO persions(name,age,address) VALUES (<span class="string">'张三'</span>,<span class="number">25</span>,<span class="string">'北京'</span>);</span><br><span class="line"># 方式2 不推荐,如果使用这种方式, 就要字段和表的字段数量完全相同</span><br><span class="line">sqlite&gt; INSERT INTO persions VALUES (<span class="number">2</span>,<span class="string">'李四'</span>,<span class="number">30</span>,<span class="string">'天津'</span>);</span><br><span class="line"># 方式3 一般不会用,向表中插入默认值,如果没有指定默认值,就写入 NULL</span><br><span class="line">sqlite&gt; INSERT INTO persions DEFAULT VALUES;</span><br><span class="line"></span><br><span class="line"># -- 更新数据</span><br><span class="line">sqlite&gt; UPDATE persions SET name=<span class="string">'李四'</span>,age=<span class="number">22</span>,address=<span class="string">'上海'</span> WHERE id=<span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"># -- 查询数据</span><br><span class="line">sqlite&gt; SELECT id,name,age,address FROM persions;</span><br><span class="line"></span><br><span class="line"># -- 删除数据</span><br><span class="line">sqlite&gt; DELETE FROM persions WHERE id=<span class="number">3</span>;</span><br><span class="line"><span class="comment">// 创建一个专门处理 SQLite 的 PDO类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Sqlite</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $pdo;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$pdo = null</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 创建 PDO_SQLITE DSN</span></span><br><span class="line">        <span class="keyword">if</span> ($pdo) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;pdo = $pdo;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                $<span class="keyword">this</span>-&gt;pdo = <span class="keyword">new</span> PDO(<span class="string">'sqlite:/home/bro/www/temp/test.sq3'</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (PDOException $e) &#123;</span><br><span class="line">                echo <span class="string">'数据库连接失败: '</span> . $e-&gt;getMessage();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 测试的时候将错误提示打开,否则出错了不清楚怎么回事</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;pdo-&gt;setAttribute($<span class="keyword">this</span>-&gt;pdo::ATTR_ERRMODE, $<span class="keyword">this</span>-&gt;pdo::ERRMODE_EXCEPTION);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 查询所有数据</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">fetchAll</span>(<span class="params">$sql</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;pdo-&gt;query($sql)-&gt;fetchAll();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 插入一条数据</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">insertOneData</span>(<span class="params">$sql</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;pdo-&gt;prepare($sql)-&gt;execute();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义一个打印结果的函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">p</span>(<span class="params">$val, $title = null</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>($title) &#123;</span><br><span class="line">        echo <span class="string">"&lt;br&gt;&lt;br&gt;&lt;h3&gt;&#123;$title&#125;&lt;/h3&gt;&lt;hr&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    echo <span class="string">'&lt;pre&gt;'</span>;</span><br><span class="line">    var_dump($val);</span><br><span class="line">    echo <span class="string">'&lt;/pre&gt;&lt;hr&gt;'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$sqlite = <span class="keyword">new</span> Sqlite; <span class="comment">//new 一个Sqlite对象</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 查询所有数据https://broqiang.com/posts/linux-php-sqlite</span></span><br><span class="line">$sqlQueryAll = <span class="string">"select * from persions"</span>;</span><br><span class="line"></span><br><span class="line">$allData = $sqlite-&gt;fetchAll($sqlQueryAll);</span><br><span class="line"></span><br><span class="line">p($allData,<span class="string">'所有查询结果'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 插入一条数据</span></span><br><span class="line">$sqlInsertOneData = sprintf(<span class="string">"INSERT INTO persions(name,age,address) VALUES ('%s',%d,'%s');"</span>,<span class="string">'张三'</span>,<span class="number">25</span>,<span class="string">'北京'</span>);</span><br><span class="line"></span><br><span class="line">p($sqlite-&gt;insertOneData($sqlInsertOneData) ? <span class="string">'插入成功'</span> : <span class="string">'插入失败'</span>,<span class="string">'插入一条数据'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="S-O-I-L-D-之单一职责"><a href="#S-O-I-L-D-之单一职责" class="headerlink" title="S.O.I.L.D 之单一职责"></a>S.O.I.L.D 之单一职责</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Acme\Reporting;</span><br><span class="line"></span><br><span class="line">use Auth;</span><br><span class="line">use DB;</span><br><span class="line">use Exception;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalesReporter</span></span></span><br><span class="line"><span class="class"></span>&#123;   </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取某个时间段的销售总额</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param  $startDate</span></span><br><span class="line"><span class="comment">     * @param  $endDate  </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params">$startDate, $endDate</span>) : <span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (! Auth::check()) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">'Authentication required for reporting'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $sales = $<span class="keyword">this</span>-&gt;queryDBForSalesBetween($startDate, $endDate);</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;format($sales);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 查询销售报表数据</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param  $startDate </span></span><br><span class="line"><span class="comment">     * @param  $endDate  </span></span><br><span class="line"><span class="comment">     *  </span></span><br><span class="line"><span class="comment">     * @return float         </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">queryDBForSalesBetween</span>(<span class="params">$startDate, $endDate</span>) : <span class="title">float</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> DB::table(<span class="string">'sales'</span>)-&gt;whereBetween(<span class="string">'created_at'</span>, [$startDate, $endDate])-&gt;sum(<span class="string">'charge'</span>) / <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 数据展示</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param  $sales</span></span><br><span class="line"><span class="comment">     * @return string     </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">format</span>(<span class="params">$sales</span>) : <span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"&lt;h1&gt;Sales: $sales&lt;/h1&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">测试</span><br><span class="line"></span><br><span class="line">$report = <span class="keyword">new</span> Acme\Reporting\SalesReporter();</span><br><span class="line">$report-&gt;between(</span><br><span class="line">    now()-&gt;subDays(<span class="number">10</span>), </span><br><span class="line">    now()</span><br><span class="line">);</span><br><span class="line">该例子明显违反了单一职责:</span><br><span class="line"></span><br><span class="line">授权方式发生变化时，如 API 授权，需要改动该类</span><br><span class="line">当数据库层发生变化时候，如使用 Redis 时，需要改动该类</span><br><span class="line">当展示方式发生变化时，需要改动该类</span><br><span class="line">正面示例</span><br><span class="line"></span><br><span class="line">对于上述的例子，应当作出如下的改动：</span><br><span class="line"></span><br><span class="line">不需要关心用户授权，用户授权与本类的职责无关</span><br><span class="line">数据层应当剥离出来</span><br><span class="line">展示层应当剥离出来</span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="comment">// 展示层接口</span></span><br><span class="line">interface SalesOutputInterface &#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">output</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">// 展示层实现</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">HtmlOutput</span> <span class="title">implements</span> <span class="title">SalesOutputInterface</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">output</span>(<span class="params">$sales</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;h1&gt;&#123;$sales&#125;&lt;/h1&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数据层</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalesRepository</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> DB::table(<span class="string">'sales'</span>)-&gt;whereBetween(<span class="string">'create_at'</span>, [$startDate, $endDate])-&gt;sum(<span class="string">'charge'</span>) / <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 职责类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalsReporter</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public $repo;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$repo</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;repo = $repo;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params">$startDate, $endDate, SalesOutputInterface $formater</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $sales = $<span class="keyword">this</span>-&gt;repo-&gt;between($startDate, $endDate);</span><br><span class="line">        $formater-&gt;output($sales);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">测试</span><br><span class="line"></span><br><span class="line">$report = <span class="keyword">new</span> SalsReporter(<span class="keyword">new</span> SalesRepository);</span><br><span class="line">$report-&gt;between(</span><br><span class="line">    now-&gt;subDays(<span class="number">10</span>), </span><br><span class="line">    now(),</span><br><span class="line">    <span class="keyword">new</span> HtmlOutput</span><br><span class="line">);</span><br><span class="line">结合 Laravel 的依赖注入，可以进一步简化https:<span class="comment">//learnku.com/articles/27923#topnav</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SalsReporter</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public $repo;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">SalesRepository $repo</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;repo = $repo;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">between</span>(<span class="params">$startDate, $endDate, SalesOutputInterface $formater</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $sales = $<span class="keyword">this</span>-&gt;repo-&gt;between($startDate, $endDate);</span><br><span class="line">        $formater-&gt;output($sales);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/27953</span></span><br></pre></td></tr></table></figure>
<h3 id="与微博内容分析相关的正则表达式"><a href="#与微博内容分析相关的正则表达式" class="headerlink" title="与微博内容分析相关的正则表达式"></a>与微博内容分析相关的正则表达式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">微博表情\[[\u4e00-\u9fa5A-Za-z]&#123;<span class="number">1</span>,<span class="number">8</span>&#125;\]</span><br><span class="line">微博昵称@[\u4e00-\u9fa5A-Z0<span class="number">-9</span>a-z_-]&#123;<span class="number">2</span>,<span class="number">30</span>&#125;</span><br><span class="line">微博话题#[^@&lt;&gt;#"&amp;'\r\n\t]&#123;1,49&#125;#</span><br><span class="line">微博短链接#https&#123;0,1&#125;://t.cn/[A-Z0-9a-z]&#123;6,8&#125;[/]&#123;0,1&#125;#</span><br><span class="line">网址长链接(https?|ftp|file):<span class="comment">//[-A-Za-z0-9+&amp;@#/%?=~_|!:,.;]+[-A-Za-z0-9+&amp;@#/%=~_|]</span></span><br><span class="line">https:<span class="comment">//www.playpi.org/2018121101.html</span></span><br></pre></td></tr></table></figure>
<h3 id="Warning-count-Parameter-must-be-an-array-or-an-object"><a href="#Warning-count-Parameter-must-be-an-array-or-an-object" class="headerlink" title="Warning: count(): Parameter must be an array or an object"></a>Warning: count(): Parameter must be an array or an object</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">PHP7<span class="number">.2</span>中这样写https:<span class="comment">//segmentfault.com/a/1190000017268206</span></span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">echo count(<span class="literal">null</span>);</span><br><span class="line">会报以下错误：</span><br><span class="line"></span><br><span class="line">Warning: count(): Parameter must be an array or an object that implements Countable <span class="keyword">in</span> </span><br><span class="line">但在PHP7<span class="number">.1</span>以下</span><br><span class="line">会返回<span class="number">0</span></span><br><span class="line">所以这次就坑了自己最终又从PHP7<span class="number">.2</span>降回<span class="number">7.1</span> php artisan -V 查看Laravel版本</span><br><span class="line"></span><br><span class="line">laravel <span class="number">5.4</span>和<span class="number">5.6</span>的日志是有区别的</span><br><span class="line">所以需要在config下添加一个logging.php的配置文件，代码请复制里面</span><br><span class="line">https:<span class="comment">//github.com/laravel/laravel/blob/develop/config/logging.php</span></span><br><span class="line">http:<span class="comment">//wangyapeng.me/2019/01/13/upgrad-laravel-5.5-to-5.7-log/ </span></span><br><span class="line">然后在.env下添加LOG_CHANNEL=stack这样日志就不会报错了</span><br><span class="line">在 .env 删除 APP_DEBUG_LEVEL，新增 LOG_CHANNEL=stack</span><br><span class="line">在 config/app.php 内删除 log 和 log_level 两个字段</span><br></pre></td></tr></table></figure>
<h3 id="无限级分类"><a href="#无限级分类" class="headerlink" title="无限级分类"></a>无限级分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">php artisan make:migration create_category_table --create=category</span><br><span class="line"><span class="comment">//https://segmentfault.com/a/1190000010359094</span></span><br><span class="line">Schema::create(<span class="string">'categorys'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">Blueprint $table</span>) </span>&#123;</span><br><span class="line">            $table-&gt;increments(<span class="string">'id'</span>);</span><br><span class="line">            $table-&gt;integer(<span class="string">'parent_id'</span>);</span><br><span class="line">            $table-&gt;string(<span class="string">'code'</span>);</span><br><span class="line">            $table-&gt;string(<span class="string">'name'</span>);</span><br><span class="line">            $table-&gt;string(<span class="string">'path'</span>);</span><br><span class="line">            $table-&gt;timestamps();</span><br><span class="line">        &#125;);</span><br><span class="line">php artisan migrate</span><br><span class="line"></span><br><span class="line">php artisan make: model Category -m</span><br><span class="line">use Illuminate\Database\Eloquent\Model;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Category</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">childCategory</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(<span class="string">'App\Category'</span>, <span class="string">'parent_id'</span>, <span class="string">'id'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">allChildrenCategorys</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;childCategory()-&gt;<span class="keyword">with</span>(<span class="string">'allChildrenCategorys'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$categorys = App/Category::<span class="keyword">with</span>(<span class="string">'allChildrenCategorys'</span>)-&gt;first();</span><br><span class="line">$categorys-&gt;allChildrenCategorys; </span><br><span class="line">$categorys-&gt;allChildrenCategorys-&gt;first()-&gt;allChildrenCategorys;</span><br><span class="line"> $arr = [];</span><br><span class="line">    array_walk_recursive($categories,<span class="function"><span class="keyword">function</span> (<span class="params">$v, $k</span>) <span class="title">use</span>(<span class="params">&amp;$arr</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>($k == <span class="string">'id'</span>)</span><br><span class="line">            $arr[] = $v;</span><br><span class="line">    &#125;);</span><br></pre></td></tr></table></figure>
<h3 id="本周一的时间戳"><a href="#本周一的时间戳" class="headerlink" title="本周一的时间戳"></a>本周一的时间戳</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">strtotime(<span class="string">'monday'</span>);</span><br><span class="line">因为这样会导致在非周一的时候计算到下一个周一，</span><br><span class="line">https:<span class="comment">//www.h57.pw/2018/11/27/calculate-the-timestamp-of-this-monday/</span></span><br><span class="line">所以我们要在计算之前先判定一下当前是周几，如果是周一就采用 <span class="string">'monday'</span> 非周一的时候采用<span class="string">'previous monday'</span></span><br><span class="line"><span class="keyword">if</span>(date(<span class="string">'w'</span>) == <span class="number">1</span>) &#123;</span><br><span class="line">    $time = strtotime(<span class="string">'monday'</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    $time = strtotime(<span class="string">'previous monday'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">老外认为“<span class="keyword">this</span> monday”是下周一，“last monday”是本周一</span><br><span class="line">直接<span class="string">"-2 monday"</span>就好了，或者<span class="string">"monday last week"</span></span><br><span class="line">date(<span class="string">'w'</span>)得到当前周几,由于周一至周六分别是<span class="number">1</span><span class="number">-6</span>，周日是<span class="number">0</span>。当值为<span class="number">0</span>的时候，上周一是<span class="number">13</span>天前。其余就是date(<span class="string">'w'</span>)+<span class="number">6</span>天前。</span><br><span class="line"></span><br><span class="line">$days = date(<span class="string">'w'</span>)==<span class="number">0</span>?<span class="number">13</span>:date(<span class="string">'w'</span>)+<span class="number">6</span>;</span><br><span class="line"></span><br><span class="line">echo date(<span class="string">'Y-m-d'</span>,time()-$days*<span class="number">86400</span>);</span><br><span class="line">&gt;&gt;&gt; date(<span class="string">'Ymd His'</span>,strtotime(<span class="string">'-1 monday'</span>))</span><br><span class="line">=&gt; <span class="string">"20190429 000000"</span></span><br><span class="line">&gt;&gt;&gt; date(<span class="string">'Ymd His'</span>,strtotime(<span class="string">'-1 monday 2019-05-06'</span>))</span><br><span class="line">=&gt; <span class="string">"20190429 000000"</span></span><br><span class="line">&gt;&gt;&gt; date(<span class="string">'Ymd His'</span>,strtotime(<span class="string">' monday 2019-05-06'</span>))</span><br><span class="line">=&gt; <span class="string">"20190506 000000"</span></span><br><span class="line">echo <span class="string">'&lt;br&gt;上周:&lt;br&gt;'</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">0</span>, <span class="number">0</span> , <span class="number">0</span>,date(<span class="string">"m"</span>),date(<span class="string">"d"</span>)-date(<span class="string">"w"</span>)+<span class="number">1</span><span class="number">-7</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">23</span>,<span class="number">59</span>,<span class="number">59</span>,date(<span class="string">"m"</span>),date(<span class="string">"d"</span>)-date(<span class="string">"w"</span>)+<span class="number">7</span><span class="number">-7</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo <span class="string">'&lt;br&gt;本周:&lt;br&gt;'</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">0</span>, <span class="number">0</span> , <span class="number">0</span>,date(<span class="string">"m"</span>),date(<span class="string">"d"</span>)-date(<span class="string">"w"</span>)+<span class="number">1</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">23</span>,<span class="number">59</span>,<span class="number">59</span>,date(<span class="string">"m"</span>),date(<span class="string">"d"</span>)-date(<span class="string">"w"</span>)+<span class="number">7</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line"> </span><br><span class="line">echo <span class="string">'&lt;br&gt;上月:&lt;br&gt;'</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">0</span>, <span class="number">0</span> , <span class="number">0</span>,date(<span class="string">"m"</span>)<span class="number">-1</span>,<span class="number">1</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">23</span>,<span class="number">59</span>,<span class="number">59</span>,date(<span class="string">"m"</span>) ,<span class="number">0</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo <span class="string">'&lt;br&gt;本月:&lt;br&gt;'</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">0</span>, <span class="number">0</span> , <span class="number">0</span>,date(<span class="string">"m"</span>),<span class="number">1</span>,date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo date(<span class="string">"Y-m-d H:i:s"</span>,mktime(<span class="number">23</span>,<span class="number">59</span>,<span class="number">59</span>,date(<span class="string">"m"</span>),date(<span class="string">"t"</span>),date(<span class="string">"Y"</span>))),<span class="string">"\n"</span>;</span><br><span class="line"> </span><br><span class="line">$getMonthDays = date(<span class="string">"t"</span>,mktime(<span class="number">0</span>, <span class="number">0</span> , <span class="number">0</span>,date(<span class="string">'n'</span>)+(date(<span class="string">'n'</span>)<span class="number">-1</span>)%<span class="number">3</span>,<span class="number">1</span>,date(<span class="string">"Y"</span>)));<span class="comment">//本季度未最后一月天数</span></span><br><span class="line">echo <span class="string">'&lt;br&gt;本季度:&lt;br&gt;'</span>;</span><br><span class="line">echo date(<span class="string">'Y-m-d H:i:s'</span>, mktime(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>,date(<span class="string">'n'</span>)-(date(<span class="string">'n'</span>)<span class="number">-1</span>)%<span class="number">3</span>,<span class="number">1</span>,date(<span class="string">'Y'</span>))),<span class="string">"\n"</span>;</span><br><span class="line">echo date(<span class="string">'Y-m-d H:i:s'</span>, mktime(<span class="number">23</span>,<span class="number">59</span>,<span class="number">59</span>,date(<span class="string">'n'</span>)+(date(<span class="string">'n'</span>)<span class="number">-1</span>)%<span class="number">3</span>,$getMonthDays,date(<span class="string">'Y'</span>))),<span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 获取指定年月的开始和结束时间戳</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param int $year 年份</span></span><br><span class="line"><span class="comment"> * @param int $month 月份</span></span><br><span class="line"><span class="comment"> * @return array(开始时间,结束时间)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getMonthBeginAndEnd</span>(<span class="params">$year = <span class="number">0</span>, $month = <span class="number">0</span></span>) </span>&#123;</span><br><span class="line">    $year = $year ? $year : date(<span class="string">'Y'</span>);</span><br><span class="line">    $month = $month ? $month : date(<span class="string">'m'</span>);</span><br><span class="line">    $d = date(<span class="string">'t'</span>, strtotime($year . <span class="string">'-'</span> . $month));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> [<span class="string">'begin'</span> =&gt; strtotime($year . <span class="string">'-'</span> . $month), <span class="string">'end'</span> =&gt; mktime(<span class="number">23</span>, <span class="number">59</span>, <span class="number">59</span>, $month, $d, $year)];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取指定时间戳所在的月份的开始时间戳和结束时间戳</span></span><br><span class="line"><span class="comment"> *http://www.phpernote.com/php-function/1439.html</span></span><br><span class="line"><span class="comment"> * @param int $timestamp</span></span><br><span class="line"><span class="comment"> * @return array(开始时间,结束时间)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getMonthBeginAndEnd</span>(<span class="params">$timestamp = <span class="number">0</span></span>) </span>&#123;</span><br><span class="line">    $timestamp = $timestamp ? $timestamp : time();</span><br><span class="line"></span><br><span class="line">    $year = date(<span class="string">'Y'</span>, $timestamp);</span><br><span class="line">    $month = date(<span class="string">'m'</span>, $timestamp);</span><br><span class="line">    $d = date(<span class="string">'t'</span>, strtotime($year . <span class="string">'-'</span> . $month));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> [<span class="string">'begin'</span> =&gt; strtotime($year . <span class="string">'-'</span> . $month), <span class="string">'end'</span> =&gt; mktime(<span class="number">23</span>, <span class="number">59</span>, <span class="number">59</span>, $month, $d, $year)];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="广度优先搜索"><a href="#广度优先搜索" class="headerlink" title="广度优先搜索"></a>广度优先搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 广度搜索</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 你的朋友关系，以及朋友的朋友的关系，查看你的朋友或者朋友的朋友是不是包含 m 结尾的名字</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 需要检索的数组</span></span><br><span class="line">$graph = [];</span><br><span class="line">$graph[<span class="string">'you'</span>] = [<span class="string">'alice'</span>, <span class="string">'bob'</span>, <span class="string">'claire'</span>];</span><br><span class="line">$graph[<span class="string">'bob'</span>] = [<span class="string">'anuj'</span>, <span class="string">'peggy'</span>];</span><br><span class="line">$graph[<span class="string">'alice'</span>] = [<span class="string">'peggy'</span>];</span><br><span class="line">$graph[<span class="string">'claire'</span>] = [<span class="string">'thom'</span>, <span class="string">'jonny'</span>];</span><br><span class="line">$graph[<span class="string">'anuj'</span>] = [];</span><br><span class="line">$graph[<span class="string">'peggy'</span>] = [];</span><br><span class="line">$graph[<span class="string">'thom'</span>] = [];</span><br><span class="line">$graph[<span class="string">'jonny'</span>] = [];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 搜索过的数组</span></span><br><span class="line">$searchedItem = [];</span><br><span class="line"><span class="comment">// 待检索的数组</span></span><br><span class="line">$waitSearchArray = [];</span><br><span class="line"><span class="comment">// 将第一层的关系加入到等待搜索的数组</span></span><br><span class="line">$waitSearchArray = array_merge($waitSearchArray, $graph[<span class="string">'you'</span>]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将结果元素赋值为 false</span></span><br><span class="line">$resultName = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//需要查找的字符</span></span><br><span class="line">$findChar = <span class="string">'z'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果等待搜索的数组不为空就循环查找</span></span><br><span class="line"><span class="keyword">while</span> ($waitSearchArray) &#123;</span><br><span class="line">    <span class="comment">// 从队列头部弹出一个元素</span></span><br><span class="line">    $name = array_shift($waitSearchArray);</span><br><span class="line">    <span class="comment">// 如果待检查的元素在已经搜索过的数组中，就跳过，这个是用来防止循环检查的</span></span><br><span class="line">    <span class="keyword">if</span> (in_array($name, $searchedItem)) &#123;</span><br><span class="line">        <span class="keyword">continue</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 获取最后一个字符</span></span><br><span class="line">    $lastChar = substr($name, strlen($name) - <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">    <span class="comment">// 如果最后一个字符是 m，就说明找到了，把结果赋值，然后跳出循环</span></span><br><span class="line">    <span class="keyword">if</span> ($lastChar == $findChar) &#123;</span><br><span class="line">        $resultName = $name;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 到这里了说明没有找到，那么把这个人的名字，放入到已经搜索过的数组中</span></span><br><span class="line">    $searchedItem[] = $name;</span><br><span class="line">    <span class="comment">// 然后再把这个名字的朋友关系加入到待搜索的数组中</span></span><br><span class="line">    $waitSearchArray = array_merge($waitSearchArray, $graph[$name]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//www.h57.pw/2017/11/14/breadthfirst-search-php-implementation/</span></span><br><span class="line">var_dump($resultName);</span><br></pre></td></tr></table></figure>
<h3 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sortArr</span>(<span class="params">$arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (count($arr) &lt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $arr;</span><br><span class="line">    &#125;</span><br><span class="line">    $mid = count($arr) / <span class="number">2</span>;</span><br><span class="line">    $arr1 = array_slice($arr, <span class="number">0</span>, $mid);</span><br><span class="line">    $arr2 = array_slice($arr, $mid, count($arr));</span><br><span class="line">    $arr1 = sortArr($arr1);</span><br><span class="line">    $arr2 = sortArr($arr2);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> mergeArr($arr1, $arr2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mergeArr</span>(<span class="params">$arr1, $arr2</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!is_array($arr1)) &#123;</span><br><span class="line">        $arr1[] = $arr1;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (!is_array($arr2)) &#123;</span><br><span class="line">        $arr2[] = $arr2;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $i =<span class="number">0</span>;</span><br><span class="line">    $j = <span class="number">0</span>;</span><br><span class="line">    $arr1Length = count($arr1);</span><br><span class="line">    $arr2Length = count($arr2);</span><br><span class="line">    $returnArr = [];</span><br><span class="line">    <span class="keyword">while</span>($i &lt; $arr1Length &amp;&amp; $j &lt; $arr2Length) &#123;</span><br><span class="line">        <span class="keyword">if</span>($arr1[$i] &gt; $arr2[$j]) &#123;</span><br><span class="line">            $returnArr[] = $arr2[$j];</span><br><span class="line">            $j++;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $returnArr[] = $arr1[$i];</span><br><span class="line">            $i++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>($tmp = $i; $tmp &lt; $arr1Length; $tmp++) &#123;</span><br><span class="line">        $returnArr[] = $arr1[$tmp];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>($tmp = $j; $tmp &lt; $arr2Length; $tmp++) &#123;</span><br><span class="line">        $returnArr[] = $arr2[$tmp];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $returnArr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$arr = [<span class="number">1</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">5</span>,<span class="number">7</span>,<span class="number">9</span>,<span class="number">3</span>,<span class="number">1</span>];</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$sortableArr = sortArr($arr);</span><br><span class="line"><span class="comment">//https://www.h57.pw/2016/09/25/php-merge-sort/</span></span><br><span class="line">foreach ($sortableArr <span class="keyword">as</span> $a) &#123;</span><br><span class="line">    print_r($a . <span class="string">"\n"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="正则表达式实现-某人"><a href="#正则表达式实现-某人" class="headerlink" title="正则表达式实现 @某人"></a>正则表达式实现 @某人</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">if (preg_match_all (‘#@\w+#u’, ‘@张全蛋 含泪质检 @三星 Note7 被炸飞，听说 @炸机 跟 @啤酒 更配哦！’, $matches)) &#123;</span><br><span class="line">var_export($matches);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出</span></span><br><span class="line">array (</span><br><span class="line"><span class="number">0</span> =&gt;</span><br><span class="line">array (</span><br><span class="line"><span class="number">0</span> =&gt; ‘@张全蛋’,</span><br><span class="line"><span class="number">1</span> =&gt; ‘@三星 Note7’,</span><br><span class="line"><span class="number">2</span> =&gt; ‘@炸机’,</span><br><span class="line"><span class="number">3</span> =&gt; ‘@啤酒’,</span><br><span class="line">),</span><br><span class="line">)</span><br><span class="line">正则表达式 #@\w+#u 中:</span><br><span class="line">#是分隔符.</span><br><span class="line">u 是修饰符，表示 Unicode.</span><br><span class="line">\w 是元字符，在 ASCII 下等价于 [A-Za-z0<span class="number">-9</span>_], 在 Unicode 下表示字符 (包括汉字) 和数字和下划线.</span><br><span class="line">+ 是量词，表示 <span class="number">1</span> 个或多个，等价于 &#123;<span class="number">1</span>,&#125;</span><br></pre></td></tr></table></figure>
<h3 id="快排和归并排序"><a href="#快排和归并排序" class="headerlink" title="快排和归并排序"></a>快排和归并排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 归并排序</span></span><br><span class="line"><span class="comment"> * @param $arr</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mergeSort</span>(<span class="params">$arr</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $arrCount = count($arr);</span><br><span class="line">    <span class="keyword">if</span> ($arrCount &lt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $arr;</span><br><span class="line">    &#125;</span><br><span class="line">    $mid = ceil($arrCount / <span class="number">2</span>); <span class="comment">// 向上取整一下</span></span><br><span class="line">    $leftArr = array_slice($arr, <span class="number">0</span>, $mid);</span><br><span class="line">    $rightArr = array_slice($arr, $mid, $arrCount - $mid);</span><br><span class="line">    <span class="keyword">return</span> mergeArr(mergeSort($leftArr), mergeSort($rightArr));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 最终合并数组的方法</span></span><br><span class="line"><span class="comment"> * @param $leftArr</span></span><br><span class="line"><span class="comment"> * @param $rightArr</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mergeArr</span>(<span class="params">$leftArr, $rightArr</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $i = <span class="number">0</span>;</span><br><span class="line">    $j = <span class="number">0</span>;</span><br><span class="line">    $returnArr = [];</span><br><span class="line">    <span class="keyword">while</span> ($i &lt; count($leftArr) &amp;&amp; $j &lt; count($rightArr)) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($leftArr[$i] &lt; $rightArr[$j]) &#123;</span><br><span class="line">            $returnArr[] = $leftArr[$i];</span><br><span class="line">            $i++;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($leftArr[$i] &gt; $rightArr[$j]) &#123;</span><br><span class="line">            $returnArr[] = $rightArr[$j];</span><br><span class="line">            $j++;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $returnArr[] = $leftArr[$i];</span><br><span class="line">            $returnArr[] = $rightArr[$j];</span><br><span class="line">            $i++;</span><br><span class="line">            $j++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> ($temp = $i; $temp &lt; count($leftArr); $temp++) &#123;</span><br><span class="line">        $returnArr[] = $leftArr[$temp];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> ($temp = $j; $temp &lt; count($rightArr); $temp++) &#123;</span><br><span class="line">        $returnArr[] = $rightArr[$temp];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $returnArr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 快速排序https://www.h57.pw/2017/04/10/thinking-about-divide-and-conquer-by-sorting-fast-merging/</span></span><br><span class="line"><span class="comment"> * @param $arr</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">quickSort</span>(<span class="params">$arr</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (count($arr) &lt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $arr;</span><br><span class="line">    &#125;</span><br><span class="line">    $flag = $arr[<span class="number">0</span>];</span><br><span class="line">    $lessArr = [];</span><br><span class="line">    $largeArr = [];</span><br><span class="line">    $flagArr = [];</span><br><span class="line">    $flagArr[] = $flag;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt; count($arr); $i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($arr[$i] &lt; $flag) &#123;</span><br><span class="line">            $lessArr[] = $arr[$i];</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($arr[$i] &gt; $flag) &#123;</span><br><span class="line">            $largeArr[] = $arr[$i];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $flagArr[] = $arr[$i];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> array_merge(quickSort($lessArr), $flagArr, quickSort($largeArr));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Jwt前后端分离"><a href="#Jwt前后端分离" class="headerlink" title="Jwt前后端分离"></a>Jwt前后端分离</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">vi composer.json </span><br><span class="line"><span class="string">"require"</span>: &#123;</span><br><span class="line">        <span class="string">"tymon/jwt-auth"</span>: <span class="string">"1.0.*"</span></span><br><span class="line">    &#125;,</span><br><span class="line"> php artisan jwt:secret</span><br><span class="line"> https:<span class="comment">//www.njphper.com/posts/cd8c881c.html</span></span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span> <span class="title">implements</span> <span class="title">AuthenticatableContract</span>, <span class="title">AuthorizableContract</span>, <span class="title">JWTSubject</span></span></span><br><span class="line"><span class="class"> </span>&#123;</span><br><span class="line">     use Authenticatable, Authorizable;</span><br><span class="line"> </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">      * The attributes that are mass assignable.</span></span><br><span class="line"><span class="comment">      *</span></span><br><span class="line"><span class="comment">      * @var array</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">     protected $fillable = [</span><br><span class="line">         <span class="string">'name'</span>, <span class="string">'email'</span>,</span><br><span class="line">     ];</span><br><span class="line"> </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">      * The attributes excluded from the model's JSON form.</span></span><br><span class="line"><span class="comment">      *</span></span><br><span class="line"><span class="comment">      * @var array</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">     protected $hidden = [</span><br><span class="line">         <span class="string">'password'</span>,</span><br><span class="line">     ];</span><br><span class="line"> </span><br><span class="line"> 	<span class="comment">/**</span></span><br><span class="line"><span class="comment"> 	 * Get the identifier that will be stored in the subject claim of the JWT.</span></span><br><span class="line"><span class="comment"> 	 *</span></span><br><span class="line"><span class="comment"> 	 * @return mixed</span></span><br><span class="line"><span class="comment"> 	 */</span></span><br><span class="line"> 	public <span class="function"><span class="keyword">function</span> <span class="title">getJWTIdentifier</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"> 	</span>&#123;</span><br><span class="line"> 		<span class="keyword">return</span> $<span class="keyword">this</span>-&gt;getKey();</span><br><span class="line"> 	&#125;</span><br><span class="line"> </span><br><span class="line"> 	<span class="comment">/**</span></span><br><span class="line"><span class="comment"> 	 * Return a key value array, containing any custom claims to be added to the JWT.</span></span><br><span class="line"><span class="comment"> 	 *</span></span><br><span class="line"><span class="comment"> 	 * @return array</span></span><br><span class="line"><span class="comment"> 	 */</span></span><br><span class="line"> 	public <span class="function"><span class="keyword">function</span> <span class="title">getJWTCustomClaims</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"> 	</span>&#123;</span><br><span class="line"> 		<span class="keyword">return</span> [];</span><br><span class="line"> 	&#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line">&amp; 引用赋值</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doloop1</span>(<span class="params">&amp;$i = <span class="number">1</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    print_r($i);</span><br><span class="line">    $i++;</span><br><span class="line">    <span class="keyword">if</span> ($i &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">        doloop1($i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">doloop1();</span><br><span class="line"><span class="keyword">static</span> 静态变量https:<span class="comment">//learnku.com/articles/28252#topnav</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doloop2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> $i = <span class="number">1</span>;</span><br><span class="line">    print_r($i);</span><br><span class="line">    $i++;</span><br><span class="line">    <span class="keyword">if</span> ($i &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">        doloop2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">doloop2();</span><br><span class="line">global 全局变量</span><br><span class="line">$i = <span class="number">1</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doloop3</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    global $i;</span><br><span class="line">    echo $i;</span><br><span class="line">    $i++;</span><br><span class="line">    <span class="keyword">if</span> ($i &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">        doloop3();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">doloop3();</span><br><span class="line">$data = [</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'title'</span> =&gt; <span class="string">'Electronics'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">2</span>, <span class="string">'title'</span> =&gt; <span class="string">'Laptops &amp; PC'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">3</span>, <span class="string">'title'</span> =&gt; <span class="string">'Laptops'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">2</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">4</span>, <span class="string">'title'</span> =&gt; <span class="string">'PC'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">2</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">5</span>, <span class="string">'title'</span> =&gt; <span class="string">'Cameras &amp; photo'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">6</span>, <span class="string">'title'</span> =&gt; <span class="string">'Camera'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">5</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">7</span>, <span class="string">'title'</span> =&gt; <span class="string">'Phones &amp; Accessories'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">8</span>, <span class="string">'title'</span> =&gt; <span class="string">'Smartphones'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">9</span>, <span class="string">'title'</span> =&gt; <span class="string">'Android'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">10</span>, <span class="string">'title'</span> =&gt; <span class="string">'iOS'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">11</span>, <span class="string">'title'</span> =&gt; <span class="string">'Other Smartphones'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">12</span>, <span class="string">'title'</span> =&gt; <span class="string">'Batteries'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">13</span>, <span class="string">'title'</span> =&gt; <span class="string">'Headsets'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">14</span>, <span class="string">'title'</span> =&gt; <span class="string">'Screen Protectors'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">        ];</span><br><span class="line">获取无限极分类</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 值引用获取无限极分类树</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * [[[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554)](https://learnku.com/users/31554) array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">make_tree</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function"></span>&#123;  </span><br><span class="line">    $refer = array();  </span><br><span class="line">    $tree = array();  </span><br><span class="line">    foreach($data <span class="keyword">as</span> $k =&gt; $v)&#123;  </span><br><span class="line">        $refer[$v[<span class="string">'id'</span>]] = &amp; $data[$k];  <span class="comment">//创建主键的数组引用  </span></span><br><span class="line">    &#125;  </span><br><span class="line"></span><br><span class="line">    foreach($data <span class="keyword">as</span> $k =&gt; $v)&#123;  </span><br><span class="line">        $parent_id = $v[<span class="string">'parent_id'</span>];   <span class="comment">//获取当前分类的父级id  </span></span><br><span class="line">        <span class="keyword">if</span>($parent_id == <span class="number">0</span>)&#123;  </span><br><span class="line">            $tree[] = &amp; $data[$k];   <span class="comment">//顶级栏目</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;  </span><br><span class="line">            <span class="keyword">if</span>(isset($refer[$parent_id]))&#123;  </span><br><span class="line">                $refer[$parent_id][<span class="string">'children'</span>][] = &amp; $data[$k];  <span class="comment">//如果存在父级栏目，则添加进父级栏目的子栏目数组中  </span></span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $tree;  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 递归获取无限极分类树</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @param int $parent_id</span></span><br><span class="line"><span class="comment"> * @param int $level</span></span><br><span class="line"><span class="comment"> * [[[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554)](https://learnku.com/users/31554) array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">make_tree2</span>(<span class="params">$data = [], $parent_id = <span class="number">0</span>, $level = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $tree = [];</span><br><span class="line">    <span class="keyword">if</span> ($data &amp;&amp; is_array($data)) &#123;</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $v) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($v[<span class="string">'parent_id'</span>] == $parent_id) &#123;</span><br><span class="line">                $tree[] = [</span><br><span class="line">                    <span class="string">'id'</span> =&gt; $v[<span class="string">'id'</span>],</span><br><span class="line">                    <span class="string">'level'</span> =&gt; $level,</span><br><span class="line">                    <span class="string">'title'</span> =&gt; $v[<span class="string">'title'</span>],</span><br><span class="line">                    <span class="string">'parent_id'</span> =&gt; $v[<span class="string">'parent_id'</span>],</span><br><span class="line">                    <span class="string">'children'</span> =&gt; make_tree2($data, $v[<span class="string">'id'</span>], $level + <span class="number">1</span>),</span><br><span class="line">                ];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel中chunk方法分块处理数据的坑"><a href="#Laravel中chunk方法分块处理数据的坑" class="headerlink" title="Laravel中chunk方法分块处理数据的坑"></a>Laravel中chunk方法分块处理数据的坑</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">chunk</span>(<span class="params">$count, callable $callback</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 我理解的是类似于limit,offset 实现数据分页查询  </span></span><br><span class="line">    $results = $<span class="keyword">this</span>-&gt;forPage($page = <span class="number">1</span>, $count)-&gt;get();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (count($results) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// On each chunk result set, we will pass them to the callback and then let the</span></span><br><span class="line">        <span class="comment">// developer take care of everything within the callback, which allows us to</span></span><br><span class="line">        <span class="comment">// keep the memory low for spinning through large result sets for working.</span></span><br><span class="line">        <span class="comment">// 如果用户回调中，更新的字段与查询的字段是一个条件，就会出现这样的问题             </span></span><br><span class="line">        <span class="keyword">if</span> (call_user_func($callback, $results) === <span class="literal">false</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $page++;</span><br><span class="line"></span><br><span class="line">        $results = $<span class="keyword">this</span>-&gt;forPage($page, $count)-&gt;get();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 1.先去查询需要更新的数据量 </span></span><br><span class="line">    $count = DB::table(<span class="string">'table'</span>)</span><br><span class="line">               -&gt;where(<span class="string">'status'</span>, <span class="string">'='</span>, <span class="number">0</span>)</span><br><span class="line">               -&gt;count();</span><br><span class="line">    echo <span class="string">"需要推送的数量:$count\r\n"</span>;</span><br><span class="line">    <span class="keyword">while</span> ($count) &#123;</span><br><span class="line">        <span class="comment">// 2.然后limit去查数据 </span></span><br><span class="line">        $data= DB::table(self::OPEN_API_WUBA_TEST_CLUE)</span><br><span class="line">                 -&gt;where(<span class="string">'is_push'</span>, <span class="string">'='</span>, <span class="attr">self</span>::IS_PUSH_FAILED)</span><br><span class="line">                 -&gt;limit(<span class="number">100</span>)</span><br><span class="line">                 -&gt;get();</span><br><span class="line">        ! empty($data) &amp;&amp; $<span class="keyword">this</span>-&gt;processData($data);</span><br><span class="line">        <span class="comment">// 3.批次处理完数据，再去查询下 需要更新的数据，直到更新完毕退出循环  </span></span><br><span class="line">        $count = DB::table(<span class="string">'table'</span>)</span><br><span class="line">                   -&gt;where(<span class="string">'status'</span>, <span class="string">'='</span>, <span class="number">0</span>)</span><br><span class="line">                   -&gt;count();</span><br><span class="line">        echo <span class="string">"剩余推送的记录数:$count\r\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    exit(<span class="string">"数据全部推送完毕.\r\n"</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">private <span class="function"><span class="keyword">function</span> <span class="title">processData</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// TODO(xiaoyi):处理数据业务逻辑https://segmentfault.com/a/1190000015284897</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="代码细节"><a href="#代码细节" class="headerlink" title="代码细节"></a>代码细节</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">一个数组对象成员，你知道怎么写吗？</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @var Ads[]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public $adsList = [];</span><br><span class="line">类的魔术方法调用的注释，你知道怎么写吗？https:<span class="comment">//yq.aliyun.com/articles/691719?spm=a2c4e.11153959.0.0.1a265ee3ENEDVQ</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @link http://manual.phpdoc.org/HTMLframesConverter/default/</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @method static int search(string $query, $limit = 10, $offset = 0)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SearchServiceProxy</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">__callStatic</span>(<span class="params">$method, $arguments</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!method_exists(<span class="string">"SearchService"</span>, $method)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> \LogicException(__CLASS__ . <span class="string">"::"</span> . $method . <span class="string">" not found"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            $data = call_user_func_array([<span class="string">"SearchService"</span>, $method], $<span class="built_in">arguments</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (\Exception $e) &#123;</span><br><span class="line">            error_log($e-&gt;getMessage());</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SearchService</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param string $query</span></span><br><span class="line"><span class="comment">     * @param int    $limit</span></span><br><span class="line"><span class="comment">     * @param int    $offset</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     * @deprecated 方法废弃</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">search</span>(<span class="params">string $query, $limit = <span class="number">10</span>, $offset = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            [<span class="string">"id"</span> =&gt; <span class="number">1</span>, <span class="string">"aaa"</span>],</span><br><span class="line">            [<span class="string">"id"</span> =&gt; <span class="number">2</span>, <span class="string">"bbb"</span>],</span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP-7-1-中使用-openssl-取代-mcrypt"><a href="#PHP-7-1-中使用-openssl-取代-mcrypt" class="headerlink" title="PHP 7.1 中使用 openssl 取代 mcrypt"></a>PHP 7.1 中使用 openssl 取代 mcrypt</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 对明文进行加密</span></span><br><span class="line"><span class="comment"> * @param string $text 需要加密的明文</span></span><br><span class="line"><span class="comment"> * @return string 加密后的密文</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">encrypt</span>(<span class="params">$text, $appid</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">        *原来代码</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        $iv = substr($<span class="keyword">this</span>-&gt;key, <span class="number">0</span>, <span class="number">16</span>);</span><br><span class="line">        $encrypted = openssl_encrypt($text,<span class="string">'AES-256-CBC'</span>,$<span class="keyword">this</span>-&gt;key,OPENSSL_ZERO_PADDING,$iv);</span><br><span class="line">        <span class="keyword">return</span> array(ErrorCode::$OK, $encrypted);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception $e) &#123;</span><br><span class="line">        <span class="comment">//print $e;</span></span><br><span class="line">        <span class="keyword">return</span> array(ErrorCode::$EncryptAESError, <span class="literal">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 对密文进行解密https://blog.wpjam.com/m/php-7-1-openssl/</span></span><br><span class="line"><span class="comment"> * @param string $encrypted 需要解密的密文</span></span><br><span class="line"><span class="comment"> * @return string 解密得到的明文</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">decrypt</span>(<span class="params">$encrypted, $appid</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        $iv = substr($<span class="keyword">this</span>-&gt;key, <span class="number">0</span>, <span class="number">16</span>);</span><br><span class="line">        $decrypted = openssl_decrypt($encrypted,<span class="string">'AES-256-CBC'</span>,$<span class="keyword">this</span>-&gt;key,OPENSSL_ZERO_PADDING,$iv);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception $e) &#123;</span><br><span class="line">        <span class="keyword">return</span> array(ErrorCode::$DecryptAESError, <span class="literal">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    *原来代码</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="static-效率优化"><a href="#static-效率优化" class="headerlink" title="static 效率优化"></a>static 效率优化</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_some_var</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">static</span> $<span class="keyword">var</span>;</span><br><span class="line">	<span class="keyword">if</span>(!isset($<span class="keyword">var</span>))&#123;</span><br><span class="line">		$<span class="keyword">var</span> = complex_calculation();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> $<span class="keyword">var</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="解码-JSONP"><a href="#解码-JSONP" class="headerlink" title="解码 JSONP"></a>解码 JSONP</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">jsonp_decode</span>(<span class="params">$jsonp, $assoc = false</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">if</span>($jsonp[<span class="number">0</span>] !== <span class="string">'['</span> &amp;&amp; $jsonp[<span class="number">0</span>] !== <span class="string">'&#123;'</span>) &#123;</span><br><span class="line">		$jsonp = substr($jsonp, strpos($jsonp, <span class="string">'('</span>));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> json_decode(trim($jsonp,<span class="string">'();'</span>), $assoc);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="合并多维数组中的子数组"><a href="#合并多维数组中的子数组" class="headerlink" title="合并多维数组中的子数组"></a>合并多维数组中的子数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$merged = call_user_func_array(<span class="string">'array_merge'</span>, $result);</span><br><span class="line">$merged = array_merge(...$result);</span><br></pre></td></tr></table></figure>
<h3 id="simplexml-load-string-的-parser-error-问题"><a href="#simplexml-load-string-的-parser-error-问题" class="headerlink" title="simplexml_load_string 的 parser error 问题"></a>simplexml_load_string 的 parser error 问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">首先使用函数 libxml_use_internal_errors() 关闭 XML 错误，然后使用 libxml_get_errors() 获取相关的错误进行自定义处理。</span><br><span class="line"></span><br><span class="line">libxml_use_internal_errors(<span class="literal">true</span>);</span><br><span class="line">$sxe = simplexml_load_string(<span class="string">"&lt;?xml version='1.0'&gt;&lt;broken&gt;&lt;xml&gt;&lt;/broken&gt;"</span>);</span><br><span class="line"><span class="keyword">if</span> (<span class="literal">false</span> === $sxe) &#123;</span><br><span class="line">    echo <span class="string">"加载 XML 错误\n"</span>;</span><br><span class="line">    foreach(libxml_get_errors() <span class="keyword">as</span> $error) &#123;</span><br><span class="line">        echo <span class="string">"\t"</span>, $error-&gt;message;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;加载 XML 错误</span><br><span class="line"> </span><br><span class="line">     Blank needed here</span><br><span class="line">     parsing XML declaration: <span class="string">'?&gt;'</span> expected</span><br><span class="line">     Opening and ending tag mismatch: xml line <span class="number">1</span> and broken</span><br><span class="line">     Premature end <span class="keyword">of</span> data <span class="keyword">in</span> tag broken line <span class="number">1</span></span><br></pre></td></tr></table></figure>
<h3 id="如何移除控制字符"><a href="#如何移除控制字符" class="headerlink" title="如何移除控制字符"></a>如何移除控制字符</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">wpjam_strip_control_characters</span>(<span class="params">$str</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> preg_replace(<span class="string">'/[\x00-\x1F\x7F-\x9F]/u'</span>, <span class="string">''</span>, $str);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取-Linux-服务器的-uptime"><a href="#获取-Linux-服务器的-uptime" class="headerlink" title="获取 Linux 服务器的 uptime"></a>获取 Linux 服务器的 uptime</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$uptime = trim(shell_exec(<span class="string">'uptime'</span>));</span><br><span class="line"><span class="comment">// output is 04:47:32 up 187 days,  5:03,  1 user,  load average: 0.55, 0.55, 0.54</span></span><br><span class="line"></span><br><span class="line">$uptime = explode(<span class="string">','</span>, $uptime);</span><br><span class="line">$uptime = explode(<span class="string">' '</span>, $uptime[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">$uptime = $uptime[<span class="number">2</span>].<span class="string">' '</span>.$uptime[<span class="number">3</span>]; <span class="comment">// 187 days</span></span><br><span class="line">$uptime = trim(file_get_contents(<span class="string">'/proc/uptime'</span>));</span><br><span class="line">$uptime = explode(<span class="string">' '</span>, $uptime);</span><br><span class="line"></span><br><span class="line">echo $uptime[<span class="number">0</span>]; <span class="comment">//uptime in seconds</span></span><br></pre></td></tr></table></figure>
<h3 id="用Memcahced-的时候，请不要把过期时间设置成超过30天"><a href="#用Memcahced-的时候，请不要把过期时间设置成超过30天" class="headerlink" title="用Memcahced 的时候，请不要把过期时间设置成超过30天"></a>用Memcahced 的时候，请不要把过期时间设置成超过30天</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">过期时间是一个 Unix 时间戳，也可以是一个从现在算起的以秒为单位的数字。</span><br><span class="line">那么怎么判断是 Unix 时间戳还是一个从现在算起的以秒为单位的数字呢？小于 <span class="number">60</span>×<span class="number">60</span>×<span class="number">24</span>×<span class="number">30</span>（<span class="number">30</span>天时间的秒数），就算是从现在算起的以秒为单位的数字。如果大于服务端会将其作为一个真实的Unix时间戳来处理而不是自当前时间的偏移。</span><br><span class="line">如果过期时间被设置为<span class="number">0</span>（默认），此元素永不过期（但是它可能由于服务端为了给其他新的元素分配空间而被删除）。</span><br><span class="line">所以如果真的要设置一个 key 的过期时间为一年后，其值应该设置为： time()+<span class="number">60</span>×<span class="number">60</span>×<span class="number">24</span>×<span class="number">365</span>。</span><br></pre></td></tr></table></figure>
<h3 id="json-decode-无法解析"><a href="#json-decode-无法解析" class="headerlink" title="json_decode 无法解析"></a>json_decode 无法解析</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">include <span class="string">'JSON.php'</span>;<span class="comment">//https://github.com/pear/Services_JSON</span></span><br><span class="line">$json = <span class="keyword">new</span> Services_JSON();</span><br><span class="line">$data = $json-&gt;decode($str);</span><br><span class="line">$json = <span class="keyword">new</span> Services_JSON(SERVICES_JSON_LOOSE_TYPE);</span><br><span class="line">$data = $json-&gt;decode($str);</span><br></pre></td></tr></table></figure>
<h3 id="cURL-报错-error-60-SSL-certificate-problem"><a href="#cURL-报错-error-60-SSL-certificate-problem" class="headerlink" title="cURL 报错 error 60 SSL certificate problem"></a>cURL 报错 error 60 SSL certificate problem</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">将 cacert.pem 文件保存在可到达的目标中。</span><br><span class="line">然后，在 php.ini 文件中，向下滚动到找到 [curl] 的位置。</span><br><span class="line">您应该看到注释掉了 CURLOPT_CAINFO 选项。 取消注释并将其指向 cacert.pem 文件。 你应该有这样的一行：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">curl.cainfo =“证书路径\cacert.pem”</span><br></pre></td></tr></table></figure>
<h3 id="按周取时间段"><a href="#按周取时间段" class="headerlink" title="按周取时间段"></a>按周取时间段</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">$start = <span class="string">'1478863624'</span>;</span><br><span class="line">$end = <span class="string">'1480505248'</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getweek</span>(<span class="params">$start, $end</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $ret = array();</span><br><span class="line">        $i = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span>($start &lt;= $end)&#123;</span><br><span class="line">            $ret[$i][<span class="string">'start'</span>] = date(<span class="string">'Y-m-d'</span>,$start);</span><br><span class="line">            $tmp = strtotime(<span class="string">"+6 days"</span>,$start);</span><br><span class="line">            <span class="keyword">if</span>($end &lt;= $tmp)</span><br><span class="line">                $ret[$i][<span class="string">'end'</span>] = date(<span class="string">'Y-m-d'</span>,$end);</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                $ret[$i][<span class="string">'end'</span>] = date(<span class="string">'Y-m-d'</span>,$tmp);</span><br><span class="line">            $i++;</span><br><span class="line">            $start = strtotime(<span class="string">"+1 day"</span>,$tmp);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $ret;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">Array</span></span><br><span class="line">(</span><br><span class="line">    [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [start] =&gt; <span class="number">2016</span><span class="number">-11</span><span class="number">-11</span></span><br><span class="line">            [end] =&gt; <span class="number">2016</span><span class="number">-11</span><span class="number">-17</span></span><br><span class="line">        )</span><br><span class="line"> </span><br><span class="line">    [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [start] =&gt; <span class="number">2016</span><span class="number">-11</span><span class="number">-18</span></span><br><span class="line">            [end] =&gt; <span class="number">2016</span><span class="number">-11</span><span class="number">-24</span></span><br><span class="line">        )</span><br><span class="line"> </span><br><span class="line">    [<span class="number">2</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [start] =&gt; <span class="number">2016</span><span class="number">-11</span><span class="number">-25</span></span><br><span class="line">            [end] =&gt; <span class="number">2016</span><span class="number">-11</span><span class="number">-30</span></span><br><span class="line">        )</span><br></pre></td></tr></table></figure>
<h3 id="PHP-代码安全"><a href="#PHP-代码安全" class="headerlink" title="PHP 代码安全"></a>PHP 代码安全</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/articles/28505</span></span><br></pre></td></tr></table></figure>
<h3 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$<span class="keyword">var</span> = <span class="string">"https://6619.io"</span>;</span><br><span class="line"></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span> (<span class="params">$var</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// echo $var;</span></span><br><span class="line">&#125;)();</span><br><span class="line"></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">$param</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// echo $param;</span></span><br><span class="line">&#125;)($<span class="keyword">var</span>);</span><br></pre></td></tr></table></figure>
<h3 id="比较字符串"><a href="#比较字符串" class="headerlink" title="比较字符串"></a>比较字符串</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">列出目录https:<span class="comment">//www.restran.net/2016/09/26/php-security-notes/</span></span><br><span class="line"></span><br><span class="line">scandir(<span class="string">'/site'</span>)</span><br><span class="line">show_source(<span class="string">'flag.php'</span>);</span><br><span class="line">highlight_file(<span class="string">'flag.php'</span>);</span><br><span class="line">var_dump(file(<span class="string">'flag.php'</span>));</span><br><span class="line">print_r(file(<span class="string">'flag.php'</span>));</span><br><span class="line">如果 GET 参数中设置 name[]=a，那么 $_GET[<span class="string">'name'</span>] = [a]，php 会把 []=a 当成数组传入， $_GET 会自动对参数调用 urldecode。</span><br><span class="line"></span><br><span class="line">$_POST 同样存在此漏洞，提交的表单数据，user[]=admin，$_POST[<span class="string">'user'</span>] 得到的是 [<span class="string">'admin'</span>] 是一个数组。</span><br><span class="line">sha1([]) === <span class="literal">false</span></span><br><span class="line">md5([]) === <span class="literal">false</span></span><br><span class="line"></span><br><span class="line">$password = <span class="string">"ffifdyop"</span>;</span><br><span class="line">$sql = <span class="string">"SELECT * FROM admin WHERE pass = '"</span>.md5($password,<span class="literal">true</span>).<span class="string">"'"</span></span><br><span class="line">b<span class="string">"SELECT * FROM admin WHERE pass = ''or'6É]™é!r,ùíb\x1C'"</span></span><br><span class="line"></span><br><span class="line"><span class="string">'0.999999999999999999999'</span> == <span class="number">1</span></span><br><span class="line"> <span class="literal">true</span> <span class="keyword">in</span> PHP <span class="number">4.3</span><span class="number">.0</span> - <span class="number">5.6</span>.x</span><br><span class="line"># false in 7.0.0+</span><br><span class="line"><span class="string">'0e0'</span> == <span class="string">'0x0'</span></span><br><span class="line"><span class="string">'0xABC'</span> == <span class="string">'0xabc'</span></span><br><span class="line"><span class="string">'0xABCdef'</span> == <span class="string">'0xabcDEF'</span></span><br><span class="line"><span class="string">'000000e1'</span> == <span class="string">'0x000000'</span></span><br><span class="line"><span class="string">'0xABFe1'</span> == <span class="string">'0xABFE1'</span></span><br><span class="line"><span class="string">'0xe'</span> == <span class="string">'0Xe'</span></span><br><span class="line"><span class="string">'0xABCDEF'</span> == <span class="string">'11259375'</span></span><br><span class="line"><span class="string">'0xABCDEF123'</span> == <span class="string">'46118400291'</span></span><br><span class="line"><span class="string">'0x1234AB'</span> == <span class="string">'1193131'</span></span><br><span class="line"><span class="string">'0x1234Ab'</span> == <span class="string">'1193131'</span></span><br><span class="line"></span><br><span class="line"># true in PHP 4.3.0 - 4.3.9, 5.2.1 - 5.6.x</span><br><span class="line"># false in PHP 4.3.10 - 4.4.9, 5.0.3 - 5.2.0, 7.0.0+</span><br><span class="line"><span class="string">'0xABCdef'</span> == <span class="string">' 0xabcDEF'</span></span><br><span class="line"><span class="string">'1e1'</span> == <span class="string">'0xa'</span></span><br><span class="line"><span class="string">'0xe'</span> == <span class="string">' 0Xe'</span></span><br><span class="line"><span class="string">'0x123'</span> == <span class="string">' 0x123'</span></span><br><span class="line"></span><br><span class="line"># true in PHP 4.3.10 - 4.4.9, 5.0.3 - 5.2.0</span><br><span class="line"># false in PHP 4.3.0 - 4.3.9, 5.0.0 - 5.0.2, 5.2.1 - 5.6.26, 7.0.0+</span><br><span class="line"><span class="string">'0e0'</span> == <span class="string">'0x0a'</span></span><br><span class="line"></span><br><span class="line"># true in PHP 4.3.0 - 4.3.9, 5.0.0 - 5.0.2</span><br><span class="line"># false in PHP 4.3.10 - 4.4.9, 5.0.3 - 5.6.26, 7.0.0+</span><br><span class="line"><span class="string">'0xe'</span> == <span class="string">' 0Xe.'</span></span><br><span class="line">var_dump(is_numeric(<span class="string">"\01"</span>)); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line">typeid=<span class="number">1</span>’ union select.. 也能通过 in_array 的验证</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> (in_array($_GET(<span class="string">'typeid'</span>], array(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>))) &#123;</span><br><span class="line">    $sql=<span class="string">"select …. where typeid="</span>.$_GET[<span class="string">'typeid'</span>]<span class="string">";</span></span><br><span class="line"><span class="string">    echo $sql;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="md5"><a href="#md5" class="headerlink" title="md5"></a>md5</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line">两个 md5 一样的字符串</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="keyword">from</span> binascii <span class="keyword">import</span> unhexlify</span><br><span class="line"><span class="keyword">from</span> hashlib <span class="keyword">import</span> md5</span><br><span class="line"><span class="keyword">from</span> future.moves.urllib.parse <span class="keyword">import</span> quote</span><br><span class="line"></span><br><span class="line">input1 = <span class="string">'Oded Goldreich\nOded Goldreich\nOded Goldreich\nOded Go'</span> + unhexlify(</span><br><span class="line"><span class="string">'d8050d0019bb9318924caa96dce35cb835b349e144e98c50c22cf461244a4064bf1afaecc5820d428ad38d6bec89a5ad51e29063dd79b16cf67c12978647f5af123de3acf844085cd025b956'</span>)</span><br><span class="line"></span><br><span class="line">print(quote(input1))</span><br><span class="line">print md5(input1).hexdigest()</span><br><span class="line"></span><br><span class="line">input2 = <span class="string">'Neal Koblitz\nNeal Koblitz\nNeal Koblitz\nNeal Koblitz\n'</span> + unhexlify(<span class="string">'75b80e0035f3d2c909af1baddce35cb835b349e144e88c50c22cf461244a40e4bf1afaecc5820d428ad38d6bec89a5ad51e29063dd79b16cf6fc11978647f5af123de3acf84408dcd025b956'</span>)</span><br><span class="line">print md5(input2).hexdigest()</span><br><span class="line">print(quote(input2))</span><br><span class="line"><span class="keyword">from</span> array <span class="keyword">import</span> array</span><br><span class="line"><span class="keyword">from</span> hashlib <span class="keyword">import</span> md5</span><br><span class="line">input1 = array(<span class="string">'I'</span>, [<span class="number">0x6165300e</span>,<span class="number">0x87a79a55</span>,<span class="number">0xf7c60bd0</span>,<span class="number">0x34febd0b</span>,<span class="number">0x6503cf04</span>,<span class="number">0x854f709e</span>,<span class="number">0xfb0fc034</span>,<span class="number">0x874c9c65</span>,<span class="number">0x2f94cc40</span>,<span class="number">0x15a12deb</span>,<span class="number">0x5c15f4a3</span>,<span class="number">0x490786bb</span>,<span class="number">0x6d658673</span>,<span class="number">0xa4341f7d</span>,<span class="number">0x8fd75920</span>,<span class="number">0xefd18d5a</span>])</span><br><span class="line">input2 = array(<span class="string">'I'</span>, [x^y <span class="keyword">for</span> x,y <span class="keyword">in</span> zip(input1, [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>&lt;&lt;<span class="number">10</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>&lt;&lt;<span class="number">31</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>])])</span><br><span class="line">print(input1 == input2) # False</span><br><span class="line">print(md5(input1).hexdigest()) # cee9a457e790cf20d4bdaa6d69f01e41</span><br><span class="line">print(md5(input2).hexdigest()) # cee9a457e790cf20d4bdaa6d69f01e41</span><br><span class="line"></span><br><span class="line"><span class="comment">// '0e5093234' 为 0，'0eabc3234' 不为 0</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">// true</span></span><br><span class="line"><span class="string">'0e509367213418206700842008763514'</span> == <span class="string">'0e481036490867661113260034900752'</span></span><br><span class="line"><span class="comment">// true</span></span><br><span class="line"><span class="string">'0e481036490867661113260034900752'</span> == <span class="string">'0'</span> </span><br><span class="line"> </span><br><span class="line"><span class="comment">// false</span></span><br><span class="line">var_dump(<span class="string">'0'</span> == <span class="string">'0e1abcd'</span>);</span><br><span class="line"><span class="comment">// true</span></span><br><span class="line">var_dump(<span class="number">0</span> == <span class="string">'0e1abcd'</span>);</span><br><span class="line"> </span><br><span class="line">var_dump(md5(<span class="string">'240610708'</span>) == md5(<span class="string">'QNKCDZO'</span>));</span><br><span class="line">var_dump(md5(<span class="string">'aabg7XSs'</span>) == md5(<span class="string">'aabC9RqS'</span>));</span><br><span class="line">var_dump(sha1(<span class="string">'aaroZmOk'</span>) == sha1(<span class="string">'aaK1STfY'</span>));</span><br><span class="line">var_dump(sha1(<span class="string">'aaO8zKZF'</span>) == sha1(<span class="string">'aa3OFF9m'</span>));</span><br><span class="line">找出 <span class="number">0</span>e 开头的 hash 碰撞，可以用如下代码</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">$salt = <span class="string">'vunp'</span>;</span><br><span class="line">$hash = <span class="string">'0e612198634316944013585621061115'</span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">for</span> ($i=<span class="number">1</span>; $i&lt;<span class="number">100000000000</span>; $i++) &#123;</span><br><span class="line">    <span class="keyword">if</span> (md5($salt . $i) == $hash) &#123;</span><br><span class="line">        echo $i;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">echo <span class="string">'  done'</span>;</span><br><span class="line"><span class="number">0</span>e 开头，md5后面全是数字的</span><br><span class="line"><span class="number">240610708</span>: <span class="number">0e462097431906509019562988736854</span></span><br><span class="line">QLTHNDT: <span class="number">0e405967825401955372549139051580</span></span><br><span class="line">QNKCDZO: <span class="number">0e830400451993494058024219903391</span></span><br><span class="line">PJNPDWY: <span class="number">0e291529052894702774557631701704</span></span><br><span class="line">NWWKITQ: <span class="number">0e763082070976038347657360817689</span></span><br><span class="line">NOOPCJF: <span class="number">0e818888003657176127862245791911</span></span><br><span class="line">MMHUWUV: <span class="number">0e701732711630150438129209816536</span></span><br><span class="line">MAUXXQC: <span class="number">0e478478466848439040434801845361</span></span><br><span class="line">IHKFRNS: <span class="number">0e256160682445802696926137988570</span></span><br><span class="line">GZECLQZ: <span class="number">0e537612333747236407713628225676</span></span><br><span class="line">GGHMVOE: <span class="number">0e362766013028313274586933780773</span></span><br><span class="line">GEGHBXL: <span class="number">0e248776895502908863709684713578</span></span><br><span class="line">EEIZDOI: <span class="number">0e782601363539291779881938479162</span></span><br><span class="line">DYAXWCA: <span class="number">0e424759758842488633464374063001</span></span><br><span class="line">DQWRASX: <span class="number">0e742373665639232907775599582643</span></span><br><span class="line">BRTKUJZ: <span class="number">00e57640477961333848717747276704</span></span><br><span class="line">ABJIHVY: <span class="number">0e755264355178451322893275696586</span></span><br><span class="line">aaaXXAYW: <span class="number">0e540853622400160407992788832284</span></span><br><span class="line">aabg7XSs: <span class="number">0e087386482136013740957780965295</span></span><br><span class="line">aabC9RqS: <span class="number">0e041022518165728065344349536299</span></span><br><span class="line">sha1</span><br><span class="line"><span class="number">10932435112</span>: <span class="number">0e07766915004133176347055865026311692244</span></span><br><span class="line">aaroZmOk: <span class="number">0e66507019969427134894567494305185566735</span></span><br><span class="line">aaK1STfY: <span class="number">0e76658526655756207688271159624026011393</span></span><br><span class="line">aaO8zKZF: <span class="number">0e89257456677279068558073954252716165668</span></span><br><span class="line">aa3OFF9m: <span class="number">0e36977786278517984959260394024281014729</span></span><br><span class="line"></span><br><span class="line">crc32 </span><br><span class="line"><span class="number">6586</span>: <span class="number">0e817678</span></span><br><span class="line"></span><br><span class="line">pre_match 在匹配的时候会消耗较大的资源，并且默认存在贪婪匹配，如果传入一个超长的字符串，会导致 pre_match 消耗大量资源从而导致 php 超时，后面的 php 语句就不会执行。payload:</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">$code=<span class="string">"xdsec###AAAAAAAAAAAAAAAAAAA(超多个A)"</span>;</span><br><span class="line">preg_match(<span class="string">"/(\d+)\.(\d+)\.(\d+)\.(\d+)/"</span>, $code));</span><br></pre></td></tr></table></figure>
<h3 id="方法多次调用"><a href="#方法多次调用" class="headerlink" title="方法多次调用"></a>方法多次调用</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php </span><br><span class="line">https:<span class="comment">//segmentfault.com/a/1190000007210948</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyDate</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getCurrentDate</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">static</span> $current_date = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!$current_date) &#123;</span><br><span class="line">            echo <span class="string">'only run one time'</span>;</span><br><span class="line">            usleep(<span class="number">200000</span>);</span><br><span class="line">            $current_date  = date(<span class="string">'Y-m-d H:i:s'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $current_date;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getCurrentDate2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        usleep(<span class="number">200000</span>);</span><br><span class="line">        echo <span class="string">'run everytime'</span>;</span><br><span class="line">        <span class="keyword">return</span> date(<span class="string">'Y-m-d H:i:s'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$start = microtime(<span class="literal">true</span>);</span><br><span class="line">$i = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">while</span> ($i--) &#123;</span><br><span class="line">    MyDate::getCurrentDate();</span><br><span class="line">&#125;</span><br><span class="line">echo microtime(<span class="literal">true</span>) - $start;    <span class="comment">//200ms</span></span><br><span class="line"></span><br><span class="line">$start = microtime(<span class="literal">true</span>);</span><br><span class="line">$i = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">while</span> ($i--) &#123;</span><br><span class="line">    MyDate::getCurrentDate2();</span><br><span class="line">&#125;</span><br><span class="line">echo microtime(<span class="literal">true</span>) - $start;    <span class="comment">//1s</span></span><br></pre></td></tr></table></figure>
<h3 id="越权漏洞"><a href="#越权漏洞" class="headerlink" title="越权漏洞"></a>越权漏洞</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">水平越权就是同等角色下的用户，不但能够访问和操作自己私有的数据，还能访问其他人私有的数据，其根本是基于数据的访问权限。</span><br><span class="line"> # 1 删除前鉴权处理</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">destory</span>(<span class="params">$id</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $payment = Payment::find($id);</span><br><span class="line">        <span class="keyword">if</span> ($payment-&gt;user_id != $<span class="keyword">this</span>-&gt;currentUser-&gt;id) &#123;</span><br><span class="line">            <span class="keyword">return</span> ...</span><br><span class="line">        &#125;</span><br><span class="line">        $payment-&gt;<span class="keyword">delete</span>();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    # 2 参入id查询删除</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">destory</span>(<span class="params">$id</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Payment::whereUserId($<span class="keyword">this</span>-&gt;currentUser-&gt;id)-&gt;whereId($id)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    # 3 模型关联查询</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class">    </span>&#123;</span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">payments</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(<span class="string">'App\Payment'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">PaymentController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class">    </span>&#123;</span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">destory</span>(<span class="params">$id</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;currentUser-&gt;payments()-&gt;whereId($id)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">在 findPassword 里面再次验证完成邮箱校验的账户是否为当前找回密码的账号</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">check</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (checkEmail($data[<span class="string">'email'</span>], $data[<span class="string">'code'</span>])) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            ...</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">findPassword</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (checkEmail($data[<span class="string">'email'</span>], $data[<span class="string">'code'</span>])) &#123;</span><br><span class="line">                $user = User::whereEmail($data[<span class="string">'email'</span>])-&gt;first();</span><br><span class="line">                $user-&gt;password = $data[<span class="string">'new_password'</span>];</span><br><span class="line">                $user-&gt;save();</span><br><span class="line">            &#125;</span><br><span class="line">            ...</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/28505#topnav</span></span><br></pre></td></tr></table></figure>
<h3 id="限制分页条目"><a href="#限制分页条目" class="headerlink" title="限制分页条目"></a>限制分页条目</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">恶意请求者请求把 pagesize 输入 <span class="number">5000</span>,<span class="number">10000</span> 等甚至更大的数，会给数据库带来一定的压力，localhost/api/articles?pageid=<span class="number">0</span>&amp;pagesize=<span class="number">10000</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//用框架自带的分页方法</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      $builder = Article::<span class="keyword">with</span>(<span class="string">'category:id,name'</span>)-&gt;orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)-&gt;paginate(<span class="number">8</span>);</span><br><span class="line">      <span class="keyword">return</span> response()-&gt;json([<span class="string">'status'</span> =&gt; <span class="literal">true</span>, <span class="string">'count'</span> =&gt; $builder-&gt;total(), <span class="string">'articles'</span> =&gt; $builder-&gt;items()]);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="XSS"><a href="#XSS" class="headerlink" title="XSS"></a>XSS</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;?php</span><br><span class="line">        $searchQuery = $_GET[<span class="string">'q'</span>];</span><br><span class="line">        <span class="comment">/* some search magic here */</span></span><br><span class="line">    ?&gt;</span><br><span class="line">&lt;h1&gt;You searched <span class="keyword">for</span>: &lt;?php echo $searchQuery; ?&gt;&lt;/h1&gt;</span><br><span class="line"></span><br><span class="line">&lt;<span class="regexp">/body&gt;</span></span><br><span class="line"><span class="regexp">因为我们把用户的内容直接打印出来，不经过任何过滤，非法用户可以拼接 URL： search.php?q=%3Cscript%3Ealert(1)%3B%3C%2Fscript%3E</span></span><br><span class="line"><span class="regexp">PHP 渲染出来的内容如下，可以看到 Javascript 代码会被直接执行：</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;body&gt;</span></span><br><span class="line"><span class="regexp">&lt;h1&gt;You searched for: &lt;script&gt;alert(1);&lt;/</span>script&gt;&lt;/h1&gt;</span><br><span class="line">&lt;p&gt;We found: Absolutely nothing because <span class="keyword">this</span> is a demo&lt;<span class="regexp">/p&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br></pre></td></tr></table></figure>
<h3 id="CSRF"><a href="#CSRF" class="headerlink" title="CSRF"></a>CSRF</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">例如网站上有用户可以用来注销账户的链接。</span><br><span class="line"></span><br><span class="line">&lt;a href=<span class="string">"http://your-website.com/delete-account"</span>&gt;销毁账户&lt;<span class="regexp">/a&gt;</span></span><br><span class="line"><span class="regexp">如果某个用户评论：</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;img src=”http:/</span><span class="regexp">/your-website.com/</span><span class="keyword">delete</span>-account”&gt; wow</span><br><span class="line">用户将在查看此评论的时候删除他们的账号。</span><br></pre></td></tr></table></figure>
<h3 id="No-input-file-specified"><a href="#No-input-file-specified" class="headerlink" title="No input file specified"></a>No input file specified</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">2019/05/23 12:31:44 [error] 5085#5085: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: open_basedir restriction in effect. File(/home/vagrant/Code/haopai-git/public/index.php) is not within the allowed path(s): (/www/wwwroot/dev.guooo.top/:/tmp/:/proc/) in Unknown on line 0</span><br><span class="line">PHP message: PHP Warning:  Unknown: failed to open stream: Operation not permitted <span class="keyword">in</span> Unknown on line <span class="number">0</span></span><br><span class="line">Unable to open primary script: <span class="regexp">/home/</span>vagrant/Code/haopai-git/public/index.php (Operation not permitted)<span class="string">" while reading response header from upstream, client: 192.168.10.1, server: hp.hopa.cc, request: "</span>GET / HTTP/<span class="number">1.1</span><span class="string">", upstream: "</span>fastcgi:<span class="comment">//unix:/var/run/php/php7.2-fpm.sock:", host: "hp.hopa.cc"</span></span><br><span class="line">看到上面的错误，我也去网上找，网上主要是说 ngnix 里面配置 fastcgi_param 的问题，但是我觉得不对吧，因为别的项目都没有问题，单单就这个项目有问题。不知怎么了，我就注意到 <span class="string">"/www/wwwroot/"</span> 这个东西，我电脑里就不应该有这个文件夹啊，于是我就去项目里查找这个字符串，最后在 .user.ini 这个文件找到了，当我看到这个文件的时候我就意识到是他的问题，我把这个文件里面的配置删了。文件内容如下：</span><br><span class="line"></span><br><span class="line">open_basedir=***********</span><br><span class="line">https:<span class="comment">//learnku.com/articles/28858</span></span><br></pre></td></tr></table></figure>
<h3 id="一个简单的composer包"><a href="#一个简单的composer包" class="headerlink" title="一个简单的composer包"></a>一个简单的composer包</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">vi https:<span class="comment">//github.com/jianyan74/php-excel/blob/master/composer.json</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="string">"name"</span>: <span class="string">"jianyan74/php-excel"</span>,</span><br><span class="line">	<span class="string">"description"</span>: <span class="string">"php excel 导入导出"</span>,</span><br><span class="line">	<span class="string">"keywords"</span>: [<span class="string">"excel"</span>, <span class="string">"csv"</span>, <span class="string">"xlsx"</span>, <span class="string">"xls"</span>, <span class="string">"html"</span>, <span class="string">"jianyan74"</span>],</span><br><span class="line">	<span class="string">"license"</span>: <span class="string">"MIT"</span>,</span><br><span class="line">	<span class="string">"authors"</span>: [</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="string">"name"</span>: <span class="string">"jianyan74"</span></span><br><span class="line">		&#125;</span><br><span class="line">	],</span><br><span class="line">	<span class="string">"type"</span>: <span class="string">"extension"</span>,</span><br><span class="line">	<span class="string">"require"</span>: &#123;</span><br><span class="line">		<span class="string">"php"</span>: <span class="string">"&gt;=7.0"</span>,</span><br><span class="line">		<span class="string">"phpoffice/phpspreadsheet"</span>: <span class="string">"^1.3"</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="string">"autoload"</span>: &#123;</span><br><span class="line">		<span class="string">"psr-4"</span>: &#123;</span><br><span class="line">			<span class="string">"jianyan\\excel\\"</span>: <span class="string">"./src"</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vi  </span><br><span class="line">&lt;?php</span><br><span class="line">namespace jianyan\excel;</span><br><span class="line">use Exception;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Cell\Coordinate;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Spreadsheet;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Writer\Html;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Writer\Xls;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Writer\Xlsx;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Writer\Csv;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 导出导入Excel</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * Class Excel</span></span><br><span class="line"><span class="comment"> * @package jianyan\excel</span></span><br><span class="line"><span class="comment"> * @author jianyan74 &lt;751393839@qq.com&gt;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Excel</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">composer <span class="built_in">require</span> jianyan74/php-excel</span><br><span class="line"></span><br><span class="line">use jianyan\excel\Excel;</span><br><span class="line">Excel::exportData($list, $header)</span><br></pre></td></tr></table></figure>
<h3 id="jwt记录"><a href="#jwt记录" class="headerlink" title="jwt记录"></a>jwt记录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">JWT 由三部分组成：头部、数据体、签名 / 加密</span><br><span class="line"></span><br><span class="line">这三部分以 . (英文句号) 连接，注意这三部分顺序是固定的，即 header.payload.signature 如下示例：</span><br><span class="line"></span><br><span class="line">eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c</span><br><span class="line"></span><br><span class="line">Base64url 编码是 Base64 的一种针对 URL 的特定变种。因为 = 、+、/ 这个三个字符在 URL 中是有特定含义的，所以 Base64url 分别将 = 直接忽略，+ 替换成 -，/ 替换成 _</span><br><span class="line">Header 部分：</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">"alg"</span>: <span class="string">"HS256"</span>,</span><br><span class="line">  <span class="string">"typ"</span>: <span class="string">"JWT"</span></span><br><span class="line">&#125;</span><br><span class="line">Payload 部分：</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">"sub"</span>: <span class="string">"demo"</span>,</span><br><span class="line">  <span class="string">"name"</span>: <span class="string">"xfly"</span>,</span><br><span class="line">  <span class="string">"admin"</span>: <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line">根据 Header 部分的 alg 属性我们可以知道该 JWT 符合 JWS 中的规范，且签名算法是 HS256 也就是 HMAC SHA<span class="number">-256</span> 算法，那么我们就可以根据如下公式计算最后的签名部分：</span><br><span class="line"></span><br><span class="line">HMACSHA256(</span><br><span class="line">  base64UrlEncode(header) + <span class="string">"."</span> +</span><br><span class="line">  base64UrlEncode(payload),</span><br><span class="line">  secret</span><br><span class="line">)</span><br><span class="line">其中的密钥是保证签名安全性的关键，所以必须保存好，在本例中密钥是 <span class="number">123456</span>。因为有这个密钥的存在，所以即便调用方偷偷的修改了前两部分的内容，在验证环节就会出现签名不一致的情况，所以保证了安全性。</span><br><span class="line">如果有效期设置过长，意味着这个 Token 泄漏后可以被长期利用，危害较大，所以一般我们都会设置一个较短的有效期。由于有效期较短，意味着需要经常进行重新授权的操作。</span><br><span class="line">假设在用户操作过程中升级 / 变更了某些权限，势必需要刷新以更新数据。</span><br><span class="line">要解决这个问题，需要在服务端部署额外逻辑，常见的做法是增加刷新机制和黑名单机制，通过 Refresh Token 刷新 JWT，将需要废弃的 Token 加入到黑名单。</span><br><span class="line">简单科普下非对称式加密算法：有两个密钥，一个公开密钥和一个私有密钥，私钥参与加密，公钥用于解密，巧妙之处是解密只能用公钥来解，即便是加密用的密钥也无法对密文进行解密。你可以看到加密和解密需要两个不同的密钥，故称之为非对称加密。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/28909#reply91329</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-FPM-创建慢日志"><a href="#PHP-FPM-创建慢日志" class="headerlink" title="PHP-FPM 创建慢日志"></a>PHP-FPM 创建慢日志</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">CGI: 是一个协议，规定了 Web 服务器和后端语言的交互。但是性能差点，每个请求都会 fork 一个新的进程。\</span><br><span class="line">FastCGI: 也是一个协议，是 CGI 的升级版，可以在一个进程内处理多个请求 \</span><br><span class="line">FPM：FastCGI 进程管理器，是一个实现了 FastCGI 协议的工具 \</span><br><span class="line">PHP-FPM: 是一个 PHP 的进程管理器，专门给 PHP 使用的 FPM 工具 \</span><br><span class="line"></span><br><span class="line"> Nginx 并不是直接和 PHP 进行通信的，而是通过 PHP-FPM。Nginx 不仅仅是一个强大的 Web 服务器，也是一个强大的代理服务器，提供了很多请求协议的代理。比如 Http 协议还有 FastCgi 协议等。</span><br><span class="line"></span><br><span class="line">当请求进入到 Nginx 中，Nginx 提供了一个 FastCgi 模块 来把 Http 请求映射为对应的 Fastcgi 请求。该模块提供了 fastcgi_param 指定来完成映射关系。它的主要作用就是把 Nginx 中的变量翻译成 PHP 中能够理解的变量。 一般该文件是在 Nginx 的安装目录下，我的内容如下:</span><br><span class="line"></span><br><span class="line">/etc/nginx # cat /etc/nginx/fastcgi_params </span><br><span class="line"></span><br><span class="line">fastcgi_param  QUERY_STRING       $query_string;</span><br><span class="line">fastcgi_param  REQUEST_METHOD     $request_method;</span><br><span class="line">fastcgi_param  CONTENT_TYPE       $content_type;</span><br><span class="line"></span><br><span class="line">.php 结尾的请求都交给 fastcgi 模块处理，然后把处理后的请求发送给 PHP-FPM，然后 PHP-FPM 把请求交给 worker 进程，worker 进程加载 PHP 解析器运行 PHP 处理结果。 其中 fastcgi_pass unix:<span class="regexp">/var/</span>run/php/php7<span class="number">.1</span>-fpm.sock; 这一行用来指定 fpm 的地址</span><br><span class="line"></span><br><span class="line">Nginx 和 PHP 的通信流程大概如下。</span><br><span class="line"></span><br><span class="line">客户端发送请求到 Nginx</span><br><span class="line">加载 nginx.conf 文件，把所有 .php 结尾的请求特殊处理</span><br><span class="line">加载 FastCGI 模块，完成请求参数的解析映射，生成 FastCGI 请求</span><br><span class="line">然后通过 fastcgi_pass 参数把 FastCGI 请求发送给 PHP-FPM 处理</span><br><span class="line">PHP-FPM 收到请求，分配给空闲 worker 子进程</span><br><span class="line">worker 子进程加载 PHP 解析器等 完成 PHP 执行获取结果</span><br><span class="line">PHP-FPM 是一种 master/worker 进程架构。首先会启动一个 master 主进程，主要功能用来完成 PHP 环境的初始化，事件监听，子进程状态管理等等。然后会启动若干 worker 子进程来处理 PHP 请求</span><br><span class="line">测试 php-fpm 配置内容是否正确 使用 -t 参数， 还可以通过加 -c 指定 php.ini 文件，通过 -y 指定 php-fpm.conf</span><br><span class="line">find / -name php-fpm7</span><br><span class="line">/etc/init.d/php-fpm7</span><br><span class="line">/etc/logrotate.d/php-fpm7</span><br><span class="line">/usr/sbin/php-fpm7</span><br><span class="line"></span><br><span class="line">/usr/sbin/php-fpm7 -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -t</span><br><span class="line"></span><br><span class="line">去看 PHP-FPM 的错误日志。在 php-fpm.conf 中增加 error_log = <span class="regexp">/xx/</span>xx/php-fpm.error.log, 然后重启 PHP-FPM，在访问测试连接请求，发现果然有报错</span><br><span class="line"></span><br><span class="line">[<span class="number">17</span>-May<span class="number">-2019</span> <span class="number">10</span>:<span class="number">04</span>:<span class="number">50</span>] NOTICE: fpm is running, pid <span class="number">12</span></span><br><span class="line">[<span class="number">17</span>-May<span class="number">-2019</span> <span class="number">10</span>:<span class="number">04</span>:<span class="number">50</span>] NOTICE: ready to handle connections</span><br><span class="line">[<span class="number">17</span>-May<span class="number">-2019</span> <span class="number">10</span>:<span class="number">05</span>:<span class="number">04</span>] ERROR: failed to ptrace(ATTACH) child <span class="number">22</span>: Operation not permitted (<span class="number">1</span>)</span><br><span class="line">[<span class="number">17</span>-May<span class="number">-2019</span> <span class="number">10</span>:<span class="number">05</span>:<span class="number">04</span>] WARNING: [pool www123] child <span class="number">22</span>, script <span class="string">'/app/www/public/index.php'</span> (request: <span class="string">"GET /index.php"</span>) executing too slow (<span class="number">2.317563</span> sec), logging</span><br><span class="line">显示子进程权限不够，ptrace 调用失败！  master 进程为了监控子进程需要调用 ptrace 来实现对子进程监控和追踪，但是调用 ptrace 却失败了  https:<span class="comment">//learnku.com/articles/28683</span></span><br></pre></td></tr></table></figure>
<h3 id="正则表达式之难点"><a href="#正则表达式之难点" class="headerlink" title="正则表达式之难点"></a>正则表达式之难点</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line">断言 ?&lt;= 这个是对需要匹配的目标左边的（前面）的进行断言，断定它前面会出现的 但是不会被匹配到。如：</span><br><span class="line">$subject = 'I am Lancer, Please say hello Lancer';</span><br><span class="line"></span><br><span class="line">//目标： 我要把hello 后面的Lancer  改为  '!' .</span><br><span class="line">$pattern = '/(?&lt;=hello )Lancer/';</span><br><span class="line">$result = preg_replace($pattern, '', $subject);</span><br><span class="line">echo $result;  //I am Lancer, Please say hello !</span><br><span class="line">这样就成功咯~</span><br><span class="line">?=,与上面的位置刚好相反，这个是对需要匹配的目标右边的（后面）的进行断言，断定它后面会出现的 但是不会被匹配到。如：</span><br><span class="line">$subject = 'I love you! I love her too!';</span><br><span class="line"></span><br><span class="line">//目标：不能爱这么多， 把第二个 'love' 改为 'hate'</span><br><span class="line">$pattern = '/love(?= her)/';</span><br><span class="line">$result = preg_replace($pattern, 'hate', $subject);</span><br><span class="line">echo $result;  //'I love you! I hate her too!'</span><br><span class="line">?&lt;!这个是需要对匹配左边的（前面的）进行断言，不过它是非，找到不是这个的。还是拿第一个例子来说：</span><br><span class="line">$subject = 'I am Lancer, Please say hello Lancer';</span><br><span class="line"></span><br><span class="line">//目标： 我还是要把hello 后面的Lancer  改为  '!'  该怎么做</span><br><span class="line">$pattern = '/(?&lt;!am )Lancer/';  //找到‘Lancer’前面不是'am '的'Lancer'</span><br><span class="line">$result = preg_replace($pattern, '', $subject);</span><br><span class="line">echo $result;  //I am Lancer, Please say hello !</span><br><span class="line">?!还是一样的秘方，还是一样的味道~</span><br><span class="line">$subject = 'I love you! I love her too!';</span><br><span class="line"></span><br><span class="line">//目标：不能爱这么多， 把第二个 'love' 改为 'hate'</span><br><span class="line">$pattern = '/love(?! you)/';</span><br><span class="line">$result = preg_replace($pattern, 'hate', $subject);</span><br><span class="line">echo $result;  //'I love you! I hate her too!'</span><br><span class="line">总结：这个断言，作用主要在，对于很多同样的目标，可是我只要其中的一个，或者多个的时候，那么就可以根据它的前面和后面，进行断言，来区分他们找到自己想要匹配的目标。</span><br><span class="line">捕获 先来说一下， 什么叫捕获。就是匹配之后，会根据你正则表达式中的（）来进行分组。一一捕获。打个比方：</span><br><span class="line">//为了显示方便，写了个show函数</span><br><span class="line">function show($str)</span><br><span class="line">&#123;</span><br><span class="line">    if (empty($str)) &#123;</span><br><span class="line">        echo null;</span><br><span class="line">    &#125; elseif (is_array($str) || is_object($str)) &#123;</span><br><span class="line">        echo '&lt;pre&gt;';</span><br><span class="line">        print_r($str);</span><br><span class="line">        echo '&lt;/pre&gt;';</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        echo $str;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">//--------------------------------------------------------------------</span><br><span class="line">$subject  = '12323abcdea1233';</span><br><span class="line">$pattern = '/(a)(b)(c)(d)(e)/';</span><br><span class="line">preg_match_all($pattern, $subject, $matches);</span><br><span class="line">show($matches);&gt;&gt;&gt; $matches   </span><br><span class="line">               =&gt; [           </span><br><span class="line">                    [         </span><br><span class="line">                      "abcde",</span><br><span class="line">                    ],        </span><br><span class="line">                    [         </span><br><span class="line">                      "a",    </span><br><span class="line">                    ],        </span><br><span class="line">                    [         </span><br><span class="line">                      "b",    </span><br><span class="line">                    ],        </span><br><span class="line">                    [         </span><br><span class="line">                      "c",    </span><br><span class="line">                    ],        </span><br><span class="line">                    [         </span><br><span class="line">                      "d",    </span><br><span class="line">                    ],        </span><br><span class="line">                    [         </span><br><span class="line">                      "e",    </span><br><span class="line">                    ],        </span><br><span class="line">                  ]           </span><br><span class="line">//这个答案，大家应该都知道吧。索引为0的是整个match的内容，接着的</span><br><span class="line">//就是捕获的每一个（）分组的内容。我们还可以这样来写：</span><br><span class="line"></span><br><span class="line">$subject = '123abcabc123';</span><br><span class="line">$pattern = '/(a)(b)(c)(\1)(\2)(\3)/';</span><br><span class="line">preg_match_all($pattern, $subject, $matches);</span><br><span class="line">show($matches);//??</span><br><span class="line">先看答案：</span><br><span class="line">Array</span><br><span class="line">(</span><br><span class="line">    [0] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; abcabc</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [1] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; a</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [2] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; b</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [3] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; c</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [4] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; a</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [5] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; b</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [6] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; c</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">)</span><br><span class="line">//你可能会有疑问， 咦,,, 怎么(\1)和(a)， (\2)和(b)，(\3)和(c) 在正则里是一样的呢？</span><br><span class="line">//其实 (a)就是指的第一组, 然后后面就可以用(\1)来表示。(b),(c)也一样。</span><br><span class="line">有人可能就会问了， 那你写这个的作用又是什么呢 ？ 获取这些括号里的干啥。。 我只要第一个索引的匹配就够了呀。</span><br><span class="line"></span><br><span class="line">但是， 你考虑到了替换这个因素没？ 如果我替换的时候需要（）的东西呢？ 这个时候，我们就可以用到捕获到的（）的东西来穿插。</span><br><span class="line">不知道有人好奇过没，为什么用那些TP框架，Laravel框架， 或者smarty 在模版里写的&#123;&#123;$msg&#125;&#125;为什么也能输出呢？ 其实就是用了正则替换~ 看代码：</span><br><span class="line"></span><br><span class="line">$msg = "正则捕获";</span><br><span class="line">$subject = '&lt;p&gt;&#123;&#123;$msg&#125;&#125;&lt;/p&gt;';</span><br><span class="line">$pattern = '/\&#123;\&#123;(.*/)\&#125;\&#125;/';  //因为正则里也有'&#123;' 和'&#125;'所以需要用‘\’转义</span><br><span class="line">$result = preg_replace($pattern, '&lt;?php echo $1; ?&gt;', $subject);</span><br><span class="line">show($result); // &lt;p&gt;&lt;?php echo $msg;?&gt;&lt;/p&gt; </span><br><span class="line">//成功修改~</span><br><span class="line">上面说的是捕获， 但是我可能不想捕获怎么办？ 那么就可以用(?:) 在前面加上?:即可。注意， 这个不会影响匹配 只会影响捕获。 如：</span><br><span class="line"></span><br><span class="line">$subject = 'abc';</span><br><span class="line">$pattern = '/(a)(?:b)(c)/';</span><br><span class="line">preg_match_all($pattern, $subject, $matches);</span><br><span class="line">show($matches);</span><br><span class="line">//结果：</span><br><span class="line">Array</span><br><span class="line">(</span><br><span class="line">    [0] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; abc</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [1] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; a</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [2] =&gt; Array</span><br><span class="line">        (</span><br><span class="line">            [0] =&gt; c</span><br><span class="line">        )</span><br><span class="line">)</span><br><span class="line">//看 匹配的结果让然是'abc' 不过没有捕获到 'b'</span><br><span class="line">https://coffeephp.com/articles/5</span><br></pre></td></tr></table></figure>
<h3 id="S-O-I-L-D-之接口隔离"><a href="#S-O-I-L-D-之接口隔离" class="headerlink" title="S.O.I.L.D 之接口隔离"></a>S.O.I.L.D 之接口隔离</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/articles/29130</span></span><br><span class="line">interface WorkableInterface</span><br><span class="line">&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">interface</span> <span class="title">ManageableInterface</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">beManaged</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">HumanWorker</span> <span class="title">implements</span> <span class="title">WorkableInterface</span>, <span class="title">SleepableInterface</span>, <span class="title">ManageableInterface</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'human working.'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">sleep</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'human sleeping'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">beManaged</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;work();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;sleep();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AndroidWorker</span> <span class="title">implements</span> <span class="title">WorkableInterface</span>, <span class="title">ManageableInterface</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'android working.'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">beManaged</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;work();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">保持 Captain 类的封闭性</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Captain</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">manage</span>(<span class="params">ManageableInterface $worker</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $worker-&gt;beManaged();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="array-map"><a href="#array-map" class="headerlink" title="array_map"></a>array_map</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$pieces = [];</span><br><span class="line">foreach($whole <span class="keyword">as</span> $item)</span><br><span class="line">&#123;</span><br><span class="line">    $pieces[] = $item[<span class="string">'foo'</span>];</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> $pieces;</span><br><span class="line"><span class="keyword">return</span> array_map(</span><br><span class="line">    <span class="function"><span class="keyword">function</span> (<span class="params">$item</span>) </span>&#123; <span class="keyword">return</span> $item[<span class="string">'foo'</span>]; &#125;,</span><br><span class="line">    $whole</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">但需要遍历 Key =&gt; Value 形式的关联数组，该怎么操作呢？</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> array_map(</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">callback</span>(<span class="params">$k, $v</span>) </span>&#123; ... &#125;,</span><br><span class="line">    array_keys($array),</span><br><span class="line">    $array</span><br><span class="line">);</span><br><span class="line">https:<span class="comment">//wi1dcard.cn/posts/php-array-map-instead-of-foreach/</span></span><br></pre></td></tr></table></figure>
<h3 id="intval-与-int"><a href="#intval-与-int" class="headerlink" title="intval() 与 (int)"></a>intval() 与 (int)</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">$int = intval(<span class="string">'0123'</span>, <span class="number">8</span>); <span class="comment">// == 83</span></span><br><span class="line">$test_int    = <span class="number">12</span>;</span><br><span class="line">$test_float  = <span class="number">12.8</span>;</span><br><span class="line">$test_string = <span class="string">"12"</span>;</span><br><span class="line"></span><br><span class="line">echo (int) $test_int;         <span class="comment">// == 12</span></span><br><span class="line">echo (int) $test_float;       <span class="comment">// == 12</span></span><br><span class="line">echo (int) $test_string;      <span class="comment">// == 12</span></span><br><span class="line"></span><br><span class="line">echo intval($test_int, <span class="number">8</span>);    <span class="comment">// == 12</span></span><br><span class="line">echo intval($test_float, <span class="number">8</span>)   <span class="comment">// == 12</span></span><br><span class="line">echo intval($test_string, <span class="number">8</span>); <span class="comment">// == 10</span></span><br><span class="line">intval() 会将类型已经是 int 或 float 的数据当作「无需转换」而直接返回！</span><br><span class="line"></span><br><span class="line">所以如上，$test_int 和 $test_float 并没有按照八进制转换，使用时一定需要注意避免踩坑。</span><br></pre></td></tr></table></figure>
<h3 id="json-encode-序列化非公开属性"><a href="#json-encode-序列化非公开属性" class="headerlink" title="json_encode() 序列化非公开属性"></a>json_encode() 序列化非公开属性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Foo</span> <span class="title">implements</span> \<span class="title">JsonSerializable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $fooProperty;</span><br><span class="line"></span><br><span class="line">    protected $barProperty;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">jsonSerialize</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            <span class="string">'fooProperty'</span> =&gt; $<span class="keyword">this</span>-&gt;fooProperty,</span><br><span class="line">            <span class="string">'barProperty'</span> =&gt; $<span class="keyword">this</span>-&gt;barProperty,</span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Foo</span> <span class="title">implements</span> \<span class="title">JsonSerializable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $fooProperty;</span><br><span class="line"></span><br><span class="line">    protected $barProperty;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">jsonSerialize</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> get_object_vars($<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;https:<span class="comment">//wi1dcard.cn/posts/json-encode-non-public-properties/</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-引用详解"><a href="#PHP-引用详解" class="headerlink" title="PHP 引用详解"></a>PHP 引用详解</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">$catList = [</span><br><span class="line">    <span class="string">'1'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">'颜色'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">    <span class="string">'2'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">2</span>, <span class="string">'name'</span> =&gt; <span class="string">'规格'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">    <span class="string">'3'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">3</span>, <span class="string">'name'</span> =&gt; <span class="string">'白色'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">    <span class="string">'4'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">4</span>, <span class="string">'name'</span> =&gt; <span class="string">'黑色'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">    <span class="string">'5'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">5</span>, <span class="string">'name'</span> =&gt; <span class="string">'大'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">2</span>],</span><br><span class="line">    <span class="string">'6'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">6</span>, <span class="string">'name'</span> =&gt; <span class="string">'小'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">2</span>],</span><br><span class="line">    <span class="string">'7'</span> =&gt; [<span class="string">'id'</span> =&gt; <span class="number">7</span>, <span class="string">'name'</span> =&gt; <span class="string">'黄色'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">];</span><br><span class="line">$treeData = [];</span><br><span class="line">foreach ($catList <span class="keyword">as</span> $item) &#123;</span><br><span class="line">    <span class="keyword">if</span> (isset($catList[$item[<span class="string">'parent_id'</span>]]) &amp;&amp; !empty($catList[$item[<span class="string">'parent_id'</span>]])) &#123;</span><br><span class="line">        <span class="comment">// 子分类</span></span><br><span class="line">        $catList[$item[<span class="string">'parent_id'</span>]][<span class="string">'children'</span>][] = &amp;$catList[$item[<span class="string">'id'</span>]];</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 一级分类</span></span><br><span class="line">        $treeData[] = &amp;$catList[$item[<span class="string">'id'</span>]];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">=&gt; [                               </span><br><span class="line">     [                             </span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">1</span>,                  </span><br><span class="line">       <span class="string">"name"</span> =&gt; <span class="string">"颜色"</span>,             </span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,           </span><br><span class="line">       <span class="string">"children"</span> =&gt; [             </span><br><span class="line">         [                         </span><br><span class="line">           <span class="string">"id"</span> =&gt; <span class="number">3</span>,              </span><br><span class="line">           <span class="string">"name"</span> =&gt; <span class="string">"白色"</span>,         </span><br><span class="line">           <span class="string">"parent_id"</span> =&gt; <span class="number">1</span>,       </span><br><span class="line">         ],                        </span><br><span class="line">         [                         </span><br><span class="line">           <span class="string">"id"</span> =&gt; <span class="number">4</span>,              </span><br><span class="line">           <span class="string">"name"</span> =&gt; <span class="string">"黑色"</span>,         </span><br><span class="line">           <span class="string">"parent_id"</span> =&gt; <span class="number">1</span>,       </span><br><span class="line">         ],                        </span><br><span class="line">         [                         </span><br><span class="line">           <span class="string">"id"</span> =&gt; <span class="number">7</span>,              </span><br><span class="line">           <span class="string">"name"</span> =&gt; <span class="string">"黄色"</span>,         </span><br><span class="line">           <span class="string">"parent_id"</span> =&gt; <span class="number">1</span>,       </span><br><span class="line">         ],                        </span><br><span class="line">       ],                          </span><br><span class="line">     ],                            </span><br><span class="line">     [                             </span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">2</span>,                  </span><br><span class="line">       <span class="string">"name"</span> =&gt; <span class="string">"规格"</span>,             </span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,           </span><br><span class="line">       <span class="string">"children"</span> =&gt; [             </span><br><span class="line">         [                         </span><br><span class="line">           <span class="string">"id"</span> =&gt; <span class="number">5</span>,              </span><br><span class="line">           <span class="string">"name"</span> =&gt; <span class="string">"大"</span>,          </span><br><span class="line">           <span class="string">"parent_id"</span> =&gt; <span class="number">2</span>,       </span><br><span class="line">         ],                        </span><br><span class="line">         [                         </span><br><span class="line">           <span class="string">"id"</span> =&gt; <span class="number">6</span>,              </span><br><span class="line">           <span class="string">"name"</span> =&gt; <span class="string">"小"</span>,          </span><br><span class="line">           <span class="string">"parent_id"</span> =&gt; <span class="number">2</span>,       </span><br><span class="line">         ],                        </span><br><span class="line">       ],                          </span><br><span class="line">     ],                            </span><br><span class="line">   ]</span><br></pre></td></tr></table></figure>
<h3 id="封装代码"><a href="#封装代码" class="headerlink" title="封装代码"></a>封装代码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (IS_POST) &#123;</span><br><span class="line">    $like = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">if</span> (isset($_POST[<span class="string">'username'</span>])) &#123;</span><br><span class="line">        $username = $_POST[<span class="string">'username'</span>];</span><br><span class="line">        $like .= <span class="string">"username like '%"</span> . $username . <span class="string">"%' and "</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (isset($_POST[<span class="string">'phone'</span>])) &#123;</span><br><span class="line">        $phone = $_POST[<span class="string">'phone'</span>];</span><br><span class="line">        $like .= <span class="string">"phone like '%"</span> . $phone . <span class="string">"%' and"</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($_POST[<span class="string">'is_auth'</span>]) &#123;</span><br><span class="line">        $isAuth = $_POST[<span class="string">'is_auth'</span>];</span><br><span class="line">        $like .= <span class="string">"is_auth like '%"</span> . $isAuth . <span class="string">"%' and"</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($_POST[<span class="string">'sex'</span>]) &#123;</span><br><span class="line">        $sex = $_POST[<span class="string">'sex'</span>];</span><br><span class="line">        $like .= <span class="string">"sex like '%"</span> . $sex . <span class="string">"%' and"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($_POST[<span class="string">'time'</span>]) &#123;</span><br><span class="line">        $time = $_POST[<span class="string">'time'</span>];</span><br><span class="line">        $like .= <span class="string">"time like '%"</span> . $time . <span class="string">"%' and"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($_POST[<span class="string">'wallet'</span>]) &#123;</span><br><span class="line">        $wallet = $_POST[<span class="string">'wallet'</span>];</span><br><span class="line">        $like .= <span class="string">"wallet like '%"</span> . $wallet . <span class="string">"%' and"</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $like = rtrim($like, <span class="string">'and'</span>);</span><br><span class="line"></span><br><span class="line">    $sql = <span class="string">"SELECT * FROM `user` WHERE &#123;$like&#125;"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> view(<span class="string">'user'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">request</span>(<span class="params">$param = null</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Request($param);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Request</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">string $param = null</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> isset($_POST[$param]) ? $_POST[$param] : <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">all</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $_POST;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $request = [</span><br><span class="line">        <span class="string">'username'</span>,</span><br><span class="line">        <span class="string">'phone'</span>,</span><br><span class="line">        <span class="string">'is_auth'</span>,</span><br><span class="line">        <span class="string">'sex'</span>,</span><br><span class="line">        <span class="string">'time'</span>,</span><br><span class="line">        <span class="string">'wallet'</span></span><br><span class="line">    ];</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (IS_POST) &#123;</span><br><span class="line">            $like = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">            foreach (request()-&gt;all() <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                <span class="keyword">if</span> (in_array($key, $<span class="keyword">this</span>-&gt;request) &amp;&amp; request($key)) &#123;</span><br><span class="line">                    $like .= sprintf(<span class="string">"%s like %s and"</span>, $key, $value);</span><br><span class="line">                    <span class="comment">//$like[] = sprintf("%s like %s", $key, $value);</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//$sql = implode(" AND ", $like);</span></span><br><span class="line">            $like = rtrim($like, <span class="string">'and'</span>);</span><br><span class="line"></span><br><span class="line">            $sql = <span class="string">"SELECT * FROM `user` WHERE &#123;$like&#125;"</span>;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> view(<span class="string">'user'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//github.com/CrazyCodes/Blog/blob/master/%E6%88%91%E4%B8%8EJetbrains%E7%9A%84%E8%BF%99%E4%BA%9B%E5%B9%B4.md</span></span><br><span class="line">https:<span class="comment">//segmentfault.com/a/1190000019274366</span></span><br></pre></td></tr></table></figure>
<h3 id="无限极分类树"><a href="#无限极分类树" class="headerlink" title="无限极分类树"></a>无限极分类树</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line">array(</span><br><span class="line">    <span class="number">1</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'中华人民共和国'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'country'</span>,</span><br><span class="line">        ),</span><br><span class="line">    <span class="number">2</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'北京市'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'province'</span>,</span><br><span class="line">        ),</span><br><span class="line">    <span class="number">20</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">20</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'天津市'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'province'</span>,</span><br><span class="line">        ),</span><br><span class="line">    <span class="number">38</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">38</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'河北省'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'province'</span>,</span><br><span class="line">        ),</span><br><span class="line">    <span class="number">218</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">218</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'山西省'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'province'</span>,</span><br><span class="line">        ),</span><br><span class="line">    <span class="number">349</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">349</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'内蒙古自治区'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'province'</span>,</span><br><span class="line">        ),</span><br><span class="line">    <span class="number">465</span> =&gt; array(</span><br><span class="line">            <span class="string">'id'</span> =&gt; <span class="number">465</span>,</span><br><span class="line">            <span class="string">'name'</span> =&gt; <span class="string">'辽宁省'</span>,</span><br><span class="line">            <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'level'</span> =&gt; <span class="string">'province'</span>,</span><br><span class="line">        ),</span><br><span class="line">    ...</span><br><span class="line">);</span><br><span class="line">优化前</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 获取以父级 ID 为 $parent_id 为根节点的树型结构数组</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* @param array $arr</span></span><br><span class="line"><span class="comment">* @param int $level 树型当前层</span></span><br><span class="line"><span class="comment">* @param int $parent_id 父级id</span></span><br><span class="line"><span class="comment">* @param int $floor 树型总层数</span></span><br><span class="line"><span class="comment">* @return array</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getList</span>(<span class="params">&amp;$arr, $level = <span class="number">0</span>, $parent_id = <span class="number">1</span>, $floor = <span class="number">3</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($level != <span class="number">0</span>) &#123;</span><br><span class="line">        $empty = $arr[$parent_id];</span><br><span class="line">        $empty[<span class="string">'list'</span>] = [];</span><br><span class="line">        $emptyPointer = &amp;$empty[<span class="string">'list'</span>];</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        $empty = [];</span><br><span class="line">        $emptyPointer = &amp;$empty;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($level &lt; $floor) &#123;</span><br><span class="line">        $ok = <span class="literal">false</span>;</span><br><span class="line">        foreach ($arr <span class="keyword">as</span> $index =&gt; &amp;$item) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($item[<span class="string">'parent_id'</span>] == $parent_id) &#123;</span><br><span class="line">                $data = self::getList($arr, $level + <span class="number">1</span>, $index);</span><br><span class="line">                array_push($emptyPointer, $data);</span><br><span class="line">                $ok = <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ($ok &amp;&amp; $item[<span class="string">'parent_id'</span>] != $parent_id) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $empty;</span><br><span class="line">&#125;</span><br><span class="line">优化后https:<span class="comment">//wi1dcard.cn/posts/php-fastest-create-tree-from-list/</span></span><br><span class="line"></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getList</span>(<span class="params">$catList</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $treeData = [];</span><br><span class="line">    foreach ($catList <span class="keyword">as</span> &amp;$item) &#123;</span><br><span class="line">        $parent_id = $item[<span class="string">'parent_id'</span>];</span><br><span class="line">        <span class="keyword">if</span> (isset($catList[$parent_id]) &amp;&amp; !empty($catList[$parent_id])) &#123;</span><br><span class="line">            $catList[$parent_id][<span class="string">'list'</span>][] = &amp;$catList[$item[<span class="string">'id'</span>]];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $treeData[] = &amp;$catList[$item[<span class="string">'id'</span>]];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    unset($item);</span><br><span class="line">    <span class="keyword">return</span> $treeData[<span class="number">0</span>][<span class="string">'list'</span>]; <span class="comment">// 根节点只有中华人民共和国，所以直接返回中国的所有子节点</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="代理与反向代理、负载均衡和缓存"><a href="#代理与反向代理、负载均衡和缓存" class="headerlink" title="代理与反向代理、负载均衡和缓存"></a>代理与反向代理、负载均衡和缓存</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">代理（正向代理，目标服务器不知道谁在访问）</span><br><span class="line"></span><br><span class="line">位于客户端和目标服务器之间，起到一个中转的作用。其实就是客户端想访问目标服务器，但是因为某些原因不能够直接访问，则把请求和目标服务器发给代理服务器，代理服务器再去请求目标服务器，把返回的响应结果返回给客户端。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">反向代理（用户实际并不知道最终服务器，只是访问一个反向代理服务器而已）</span><br><span class="line"></span><br><span class="line">客户端会把反向代理服务器当成目标服务器，向反向代理服务器发送请求后，反向代理服务器再请求内部的后端服务器，把得到的响应结果返回给客户端。</span><br><span class="line">server&#123;</span><br><span class="line">listen <span class="number">80</span>;</span><br><span class="line">  server_name test.test;</span><br><span class="line">  #将本机接收到的test.test的请求全部转发到另外一台服务器192.168.78.128</span><br><span class="line">  location /&#123;</span><br><span class="line">    proxy_pass http:<span class="comment">//192.168.78.128; </span></span><br><span class="line">    #下面是其他辅助指令</span><br><span class="line">    proxy_set_header Host $host; #更改来自客户端的请求头信息</span><br><span class="line">    proxy_set_header X-Real_IP $remote_addr;    #用户真实访问ip</span><br><span class="line">    proxy_connect_timeout 2; #配置nginx与后端服务器建立连接的超时时间</span><br><span class="line">    proxy_read_timeout 2; #配置nginx向后端发出read请求的等待响应超时时间</span><br><span class="line">    proxy_send_timeout 2; #配置nginx向后端服务器发出write请求的等待响应超时时间</span><br><span class="line">    proxy_redirect http:<span class="comment">//www.baidu.com; #用于修改后端服务器返回的响应头中的Location和Refresh</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">用户不直接访问后端服务器，而是访问负载均衡服务器，由负载均衡服务器再次转发到后端服务器。如果这个时候有一台后端服务器挂掉了，那么负载均衡服务器会剔除掉它，将后续请求都转发到好的那台，这样就不影响网站的正常运行</span><br><span class="line">server&#123;</span><br><span class="line">    listen <span class="number">80</span>;</span><br><span class="line">  server_name test.test;</span><br><span class="line">  location / &#123;</span><br><span class="line">    proxy_pass http:<span class="comment">//web_server; #反向代理</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">#配置负载均衡服务器组</span><br><span class="line">upstream web_server &#123;</span><br><span class="line">    server <span class="number">192.168</span><span class="number">.78</span><span class="number">.128</span>;</span><br><span class="line">  server <span class="number">192.168</span><span class="number">.78</span><span class="number">.129</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#配置负载均衡服务器组</span><br><span class="line">upstream web_server &#123;</span><br><span class="line">    server <span class="number">192.168</span><span class="number">.78</span><span class="number">.128</span> weight=<span class="number">1</span>;</span><br><span class="line">  server <span class="number">192.168</span><span class="number">.78</span><span class="number">.129</span> weight=<span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line">这里面的权值总和为一个循环，这里以 <span class="number">4</span> 次为一个循环，那么就是每四次请求中，三次会被分派到 <span class="number">129</span> 这个服务器，一次分配到 <span class="number">128</span>，但是具体三次并不会顺序执行，而是按照算法分散执行。https:<span class="comment">//learnku.com/articles/26686 https://learnku.com/articles/29231#reply92298</span></span><br></pre></td></tr></table></figure>
<h3 id="Swoole-Redis-list实现简单消息推送"><a href="#Swoole-Redis-list实现简单消息推送" class="headerlink" title="Swoole,Redis list实现简单消息推送"></a>Swoole,Redis list实现简单消息推送</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">swoole.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">$server = <span class="keyword">new</span> swoole_websocket_server(<span class="string">"0.0.0.0"</span>, <span class="number">9502</span>);</span><br><span class="line"></span><br><span class="line">$server-&gt;on(<span class="string">'workerStart'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$server, $workerId</span>) </span>&#123;</span><br><span class="line">    $redis = <span class="keyword">new</span> Swoole\Coroutine\Redis();</span><br><span class="line">    $redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);</span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="comment">// brpop 第二个参数 50 表示超时（阻塞等待）时间, blpop 同理，详情建议读文档,对应的 redis 操作是 rpush/lpush key content </span></span><br><span class="line">        <span class="keyword">if</span> (($message = $redis-&gt;brpop(<span class="string">'message'</span>, <span class="number">50</span>)) === <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// var_dump($message); 结果为数组 </span></span><br><span class="line">        foreach ($server-&gt;connections <span class="keyword">as</span> $fd) &#123;</span><br><span class="line">            $server-&gt;push($fd, <span class="string">'redis 的 '</span> . $message[<span class="number">0</span>] . <span class="string">' 队列发送消息:'</span> . $message[<span class="number">1</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$server-&gt;on(<span class="string">'open'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$server, $request</span>) </span>&#123;</span><br><span class="line">    $server-&gt;push($request-&gt;fd, <span class="string">"hello;\n"</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$server-&gt;on(<span class="string">'message'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">swoole_websocket_server $server, $request</span>) </span>&#123;</span><br><span class="line">    $server-&gt;push($request-&gt;fd, <span class="string">"hello"</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$server-&gt;on(<span class="string">'close'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$server, $fd</span>) </span>&#123;</span><br><span class="line">    echo <span class="string">"client-&#123;$fd&#125; is closed\n"</span>;</span><br><span class="line">    $server-&gt;close($fd);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$server-&gt;start();</span><br><span class="line"><span class="comment">//https://alpha2016.github.io/2019/02/16/PHP,Swoole,Redis-list%E5%AE%9E%E7%8E%B0%E6%B6%88%E6%81%AF%E6%8E%A8%E9%80%81/</span></span><br></pre></td></tr></table></figure>
<h3 id="Redis-list队列异常崩溃"><a href="#Redis-list队列异常崩溃" class="headerlink" title="Redis list队列异常崩溃"></a>Redis list队列异常崩溃</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">用 Redis list 做消息队列，在取出消息的时候 Redis 宕机，程序上的业务逻辑没执行，怎样处理？先不去考虑 Redis 的异常处理及恢复，一般主从哨兵机制很难崩溃，暂时考虑程序端如何处理这种</span><br><span class="line"></span><br><span class="line">直接使用 Redis 官方的 RPOPLPUSH / BRPOPLPUSH source destination 命令，在读取消息的同时，将读取到的消息内容放到目标队列，然后当前进程进行消费数据，消费成功，则 lrem destination <span class="number">1</span> key 在目标队列中删除刚才的消息内容。</span><br></pre></td></tr></table></figure>
<h3 id="从0-n之间取k个不重复的数"><a href="#从0-n之间取k个不重复的数" class="headerlink" title="从0-n之间取k个不重复的数"></a>从0-n之间取k个不重复的数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">使用 array 系列函数的方法</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRandomN</span>(<span class="params">$max, $num</span>) </span>&#123;</span><br><span class="line">    $count = <span class="number">0</span>;</span><br><span class="line">    $<span class="keyword">return</span> = array();</span><br><span class="line">    <span class="keyword">while</span> ($count &lt; $num) &#123;</span><br><span class="line">        $<span class="keyword">return</span>[] = mt_rand(<span class="number">0</span>, $max);</span><br><span class="line">        $<span class="keyword">return</span> = array_flip(array_flip($<span class="keyword">return</span>));</span><br><span class="line">        $count = count($<span class="keyword">return</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    shuffle($<span class="keyword">return</span>);</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> getRandomN(<span class="number">20</span>, <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// output:[7,14,6,12,3,4,15,0,16,10]</span></span><br><span class="line">不使用 array 系列函数的方法</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRandomN</span>(<span class="params">$num, $n</span>)</span>&#123;</span><br><span class="line">    $startArray = range(<span class="number">0</span>, $num);</span><br><span class="line">    $resultArray = [];</span><br><span class="line">    <span class="keyword">for</span>($i = <span class="number">0</span>; $i &lt; $n; $i++)</span><br><span class="line">    &#123;</span><br><span class="line">        $random = mt_rand(<span class="number">0</span>, $num - $i);</span><br><span class="line">        $resultArray[$i] = $startArray[$random];</span><br><span class="line">        $startArray[$random] = $startArray[$num - $i - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $resultArray;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> getRandomN(<span class="number">20</span>, <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// output:[7,14,6,12,3,4,15,0,16,10]</span></span><br><span class="line">https:<span class="comment">//alpha2016.github.io/2019/04/09/PHP%E4%BB%8E0-n%E4%B9%8B%E9%97%B4%E9%9A%8F%E6%9C%BA%E5%8F%96k%E4%B8%AA%E4%B8%8D%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0/</span></span><br></pre></td></tr></table></figure>
<h3 id="匿名函数-1"><a href="#匿名函数-1" class="headerlink" title="匿名函数"></a>匿名函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//声明一个狗对象</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $name = <span class="string">'旺财'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//声明一个猫对象</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $name = <span class="string">'喵喵'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//声明一个匿名函数，用来输出名字</span></span><br><span class="line">$sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo $<span class="keyword">this</span>-&gt;name;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//用bind赋值上面那个匿名函数，并指定this指向狗对象，返回新的闭包函数</span></span><br><span class="line">$sayDogName = Closure::bind($sayName, <span class="keyword">new</span> Dog);</span><br><span class="line"></span><br><span class="line"><span class="comment">//调用新的闭包函数，输出 “旺财”</span></span><br><span class="line">$sayDogName();  </span><br><span class="line"><span class="comment">//指定类的作用域为 Dog 对象</span></span><br><span class="line">$sayDogName = Closure::bind($sayName, <span class="keyword">new</span> Dog, <span class="string">'Dog'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//输出 “旺财”</span></span><br><span class="line">$sayDogName();</span><br></pre></td></tr></table></figure>
<h3 id="数组整合合并"><a href="#数组整合合并" class="headerlink" title="数组整合合并"></a>数组整合合并</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$a=[</span><br><span class="line">   [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">1</span>,</span><br><span class="line">        <span class="string">"name"</span>=&gt; <span class="string">"std_name_0"</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt;<span class="number">2</span>,</span><br><span class="line">        <span class="string">"name"</span>=&gt; <span class="string">"std_name_1"</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">3</span>,</span><br><span class="line">        <span class="string">"name"</span>=&gt;<span class="string">"std_name_2"</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">4</span>,</span><br><span class="line">        <span class="string">"name"</span>=&gt; <span class="string">"std_name_3"</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">5</span>,</span><br><span class="line">        <span class="string">"name"</span>=&gt; <span class="string">"std_name_4"</span></span><br><span class="line">    ]</span><br><span class="line"></span><br><span class="line">]</span><br><span class="line">$b=[</span><br><span class="line">[</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">2</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">3</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt; <span class="number">2</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt;<span class="number">4</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt; <span class="number">4</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">5</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt;<span class="number">6</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">6</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt; <span class="number">8</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt; <span class="number">7</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt; <span class="number">10</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">"id"</span>=&gt;<span class="number">8</span>,</span><br><span class="line">        <span class="string">"order_count"</span>=&gt; <span class="number">12</span></span><br><span class="line">    ]</span><br><span class="line">]</span><br><span class="line">$users_info=[];</span><br><span class="line">foreach ($a <span class="keyword">as</span> $val)&#123;</span><br><span class="line">    $users_info[$val[<span class="string">'id'</span>]][<span class="string">'id'</span>] = $val[<span class="string">'id'</span>];</span><br><span class="line">    $users_info[$val[<span class="string">'id'</span>]][<span class="string">'name'</span>] = $val[<span class="string">'name'</span>];</span><br><span class="line">    $users_info[$val[<span class="string">'id'</span>]][<span class="string">'order_count'</span>] = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line">foreach ($b <span class="keyword">as</span> $val)&#123;</span><br><span class="line">    $users_info[$val[<span class="string">'id'</span>]][<span class="string">'id'</span>] = $val[<span class="string">'id'</span>];</span><br><span class="line">    $users_info[$val[<span class="string">'id'</span>]][<span class="string">'order_count'</span>] = $val[<span class="string">'order_count'</span>];</span><br><span class="line">    $users_info[$val[<span class="string">'id'</span>]][<span class="string">'name'</span>] = $users_info[$val[<span class="string">'id'</span>]][<span class="string">'name'</span>]??<span class="string">''</span>;</span><br><span class="line">&#125;</span><br><span class="line">&gt;&gt;&gt; $users_info</span><br><span class="line">=&gt; [</span><br><span class="line">     <span class="number">1</span> =&gt; [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">1</span>,</span><br><span class="line">       <span class="string">"name"</span> =&gt; <span class="string">"std_name_0"</span>,</span><br><span class="line">       <span class="string">"order_count"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">     <span class="number">2</span> =&gt; [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">2</span>,</span><br><span class="line">       <span class="string">"name"</span> =&gt; <span class="string">"std_name_1"</span>,</span><br><span class="line">       <span class="string">"order_count"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">     <span class="number">3</span> =&gt; [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">3</span>,</span><br><span class="line">       <span class="string">"name"</span> =&gt; <span class="string">"std_name_2"</span>,</span><br><span class="line">       <span class="string">"order_count"</span> =&gt; <span class="number">2</span>,</span><br><span class="line">     ],</span><br><span class="line">     <span class="number">4</span> =&gt; [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">4</span>,</span><br><span class="line">       <span class="string">"name"</span> =&gt; <span class="string">"std_name_3"</span>,</span><br><span class="line">       <span class="string">"order_count"</span> =&gt; <span class="number">4</span>,</span><br><span class="line">     ],</span><br><span class="line">https:<span class="comment">//www.guaosi.com/2018/12/14/complex-two-dimensional-arrays/</span></span><br></pre></td></tr></table></figure>
<h3 id="循环中查询数据库"><a href="#循环中查询数据库" class="headerlink" title="循环中查询数据库"></a>循环中查询数据库</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">foreach ( $product <span class="keyword">as</span> $p) &#123;</span><br><span class="line">       <span class="comment">//如果存在则更新</span></span><br><span class="line">       <span class="keyword">if</span> ( $<span class="keyword">this</span>-&gt;count($p) ) &#123;</span><br><span class="line">           $<span class="keyword">this</span>-&gt;update($p);</span><br><span class="line">       &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">           $<span class="keyword">this</span>-&gt;insert($p);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">     $product = [</span><br><span class="line">           [</span><br><span class="line">               <span class="string">'product_id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">               <span class="string">'pnid'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">               <span class="string">'pvid'</span>=&gt;<span class="number">10</span></span><br><span class="line">           ],</span><br><span class="line">           [</span><br><span class="line">               <span class="string">'product_id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">               <span class="string">'pnid'</span>=&gt;<span class="number">3</span>,</span><br><span class="line">               <span class="string">'pvid'</span>=&gt;<span class="number">30</span></span><br><span class="line">           ],</span><br><span class="line">           [</span><br><span class="line">               <span class="string">'product_id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">               <span class="string">'pnid'</span>=&gt;<span class="number">12</span>,</span><br><span class="line">               <span class="string">'pvid'</span>=&gt;<span class="number">21</span></span><br><span class="line">           ],</span><br><span class="line">           [</span><br><span class="line">               <span class="string">'product_id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">               <span class="string">'pnid'</span>=&gt;<span class="number">31</span>,</span><br><span class="line">               <span class="string">'pvid'</span>=&gt;<span class="number">33</span></span><br><span class="line">           ],</span><br><span class="line">       ];</span><br><span class="line">   </span><br><span class="line">       <span class="comment">//获取product_id为1的数据</span></span><br><span class="line">       $oldData = $<span class="keyword">this</span>-&gt;get(<span class="number">1</span>);</span><br><span class="line">   </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 这里会把数据组成以pnid为键的数组</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">       $oldKeyData = array_column($oldData,NULL,<span class="string">'pnid'</span>);</span><br><span class="line">   </span><br><span class="line">       $update = [];</span><br><span class="line">       $insert = [];</span><br><span class="line">   </span><br><span class="line">       foreach ( $product <span class="keyword">as</span> $p) &#123;</span><br><span class="line">           <span class="keyword">if</span> ( isset($oldKeyData[$p[<span class="string">'pnid'</span>]]) ) &#123;</span><br><span class="line">               $updatep[] = $p;</span><br><span class="line">           &#125; eles &#123;</span><br><span class="line">               $insert[] = $p;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       <span class="comment">//进行更新和删除https://learnku.com/articles/29764</span></span><br></pre></td></tr></table></figure>
<h3 id="json排序"><a href="#json排序" class="headerlink" title="json排序"></a>json排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">array:<span class="number">5</span> [</span><br><span class="line">  <span class="number">6</span> =&gt; <span class="string">"中国"</span></span><br><span class="line">  <span class="number">5</span> =&gt; <span class="string">"美国"</span></span><br><span class="line">  <span class="number">4</span> =&gt; <span class="string">"日本"</span></span><br><span class="line">  <span class="number">1</span> =&gt; <span class="string">"俄罗斯"</span></span><br><span class="line">  <span class="number">2</span> =&gt; <span class="string">"英国"</span></span><br><span class="line">]</span><br><span class="line"> <span class="keyword">return</span> Response::send(<span class="number">0</span>,<span class="string">'成功！'</span>,$data);</span><br><span class="line">data    &#123;…&#125;</span><br><span class="line"><span class="number">1</span>   俄罗斯</span><br><span class="line"><span class="number">2</span>   英国</span><br><span class="line"><span class="number">4</span>   日本</span><br><span class="line"><span class="number">5</span>   美国</span><br><span class="line"><span class="number">6</span>   中国</span><br><span class="line">每个数字 key 前面加个空格字符串即可。</span><br></pre></td></tr></table></figure>
<h3 id="数组排序"><a href="#数组排序" class="headerlink" title="数组排序"></a>数组排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array_multisort(array_column($array,<span class="string">'sort'</span>),SORT_ASC,$array);</span><br></pre></td></tr></table></figure>
<h3 id="图片上传添加自动裁剪"><a href="#图片上传添加自动裁剪" class="headerlink" title="图片上传添加自动裁剪"></a>图片上传添加自动裁剪</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> intervention/image</span><br><span class="line"></span><br><span class="line">Intervention\Image\ImageServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line"><span class="class">'<span class="title">Image</span>' </span>=&gt; Intervention\Image\Facades\Image::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">image_upload</span>(<span class="params">Request $req</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line"><span class="keyword">if</span> ($req-&gt;isMethod(<span class="string">'POST'</span>)) &#123;</span><br><span class="line">$file = $req-&gt;file(<span class="string">'file'</span>);</span><br><span class="line"><span class="comment">// 图片宽度参数 可以写死也可传值接收 关闭 false</span></span><br><span class="line">$max_width = <span class="literal">false</span>;</span><br><span class="line"><span class="keyword">if</span> ($file-&gt;isValid()) &#123;</span><br><span class="line">$realPath = $file-&gt;getRealPath ();<span class="comment">// 临时文件路径</span></span><br><span class="line"></span><br><span class="line">    $entension = $file-&gt;getClientOriginalExtension();<span class="comment">//上传文件后缀名</span></span><br><span class="line">    $time = date(<span class="string">'Ym'</span>);</span><br><span class="line">    $newName = md5(uniqid(microtime(<span class="literal">true</span>), <span class="literal">true</span>)) . date(<span class="string">'YmdHis'</span>) . <span class="string">'.'</span> . $entension;</span><br><span class="line">    $path = $file-&gt;move(base_path() . <span class="string">'/public/upload/image/'</span> . $time, $newName);</span><br><span class="line">    $filepath = <span class="string">'/upload/image/'</span> . $time . <span class="string">'/'</span> . $newName;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 图片物理路径  特别重要 如果路径则无法识别写入图片</span></span><br><span class="line">    $img = public_path().<span class="string">'/'</span>.$filepath;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  图片剪裁逻辑  如果限制了图片宽度且不为gif格式，就进行裁剪</span></span><br><span class="line">    <span class="keyword">if</span> ($max_width &amp;&amp; $entension != <span class="string">'gif'</span>) &#123;</span><br><span class="line">        <span class="comment">// 此类中封装的函数，用于裁剪图片</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;reduceSize($img, $max_width);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> response()-&gt;json([<span class="string">'status'</span> =&gt; <span class="number">0</span>, <span class="string">'msg'</span> =&gt; <span class="string">'上传成功'</span>, <span class="string">'filepath'</span> =&gt; $filepath]);</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125; <span class="keyword">catch</span> (Exception $e) &#123;</span><br><span class="line"><span class="keyword">return</span> $<span class="keyword">this</span>-&gt;doFailure($e);</span><br><span class="line">&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 图片按宽度剪裁</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">reduceSize</span>(<span class="params">$img, $max_width</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"><span class="comment">// 先实例化，传参是文件的磁盘物理路径</span></span><br><span class="line"></span><br><span class="line">$image = Image::make($img);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 进行大小调整的操作</span></span><br><span class="line">$image-&gt;resize($max_width, <span class="literal">null</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$constraint</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设定宽度是 $max_width，高度等比例双方缩放</span></span><br><span class="line">$constraint-&gt;aspectRatio();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 防止裁图时图片尺寸变大</span></span><br><span class="line">$constraint-&gt;upsize();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对图片修改后进行保存</span></span><br><span class="line">$image-&gt;save();</span><br><span class="line">&#125;<span class="string">`</span></span><br></pre></td></tr></table></figure>
<h3 id="快速接入-GitHub-登陆"><a href="#快速接入-GitHub-登陆" class="headerlink" title="快速接入 GitHub 登陆"></a>快速接入 GitHub 登陆</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 发送请求(也可以直接使用guzzle)</span></span><br><span class="line"><span class="comment"> *https://learnku.com/articles/29160</span></span><br><span class="line"><span class="comment"> * @param string $url      请求地址</span></span><br><span class="line"><span class="comment"> * @param array $data      请求数据</span></span><br><span class="line"><span class="comment"> * @param array $headers   请求头</span></span><br><span class="line"><span class="comment"> * @return string|array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sendRequest</span>(<span class="params">$url, $data = [], $headers = []</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $ch = curl_init();</span><br><span class="line">    curl_setopt($ch, CURLOPT_URL, $url);</span><br><span class="line">    curl_setopt($ch, CURLOPT_HEADER, <span class="number">0</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, <span class="literal">false</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, <span class="literal">false</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);</span><br><span class="line">    <span class="keyword">if</span> (!empty($data)) &#123;</span><br><span class="line">        curl_setopt($ch, CURLOPT_TIMEOUT, <span class="number">60</span>);</span><br><span class="line">        curl_setopt($ch, CURLOPT_POST, <span class="number">1</span>);</span><br><span class="line">        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);</span><br><span class="line">    &#125;</span><br><span class="line">    $response = curl_exec($ch) ? curl_multi_getcontent($ch) : <span class="string">''</span>;</span><br><span class="line">    curl_close($ch);</span><br><span class="line">    <span class="keyword">return</span> $response;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果用户同意登陆， github 就会返回到 callback.php 并携带一个code参数</span></span><br><span class="line"><span class="comment">// 此时只需要使用这个 code 去获取 access_token, 然后在使用 access_token 获取用户信息</span></span><br><span class="line">$url        = <span class="string">"https://github.com/login/oauth/access_token"</span>;</span><br><span class="line">$app_id     = <span class="string">"your github oauth app client_id"</span>;</span><br><span class="line">$app_secret = <span class="string">"your github oauth app client_secret"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 组合请求参数</span></span><br><span class="line">$code   = $_GET[<span class="string">'code'</span>];</span><br><span class="line">$params = [</span><br><span class="line">    <span class="string">'client_id'</span>     =&gt; $app_id,</span><br><span class="line">    <span class="string">'client_secret'</span> =&gt; $app_secret,</span><br><span class="line">    <span class="string">'code'</span>          =&gt; $code,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 发送请求并获取响应信息</span></span><br><span class="line">$response = sendRequest($url, $params, [<span class="string">'Accept: application/json'</span>]);</span><br><span class="line">$response = json_decode($response, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果有响应信息， 说明请求成功</span></span><br><span class="line"><span class="keyword">if</span> (!empty($response[<span class="string">'access_token'</span>])) &#123;</span><br><span class="line">    <span class="comment">// 请求成功，使用 access_token 获取用户信息</span></span><br><span class="line">    $access_token = $response[<span class="string">'access_token'</span>];</span><br><span class="line">    $url = <span class="string">"https://api.github.com/user"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发送请求，调取github API 获取用户信息</span></span><br><span class="line">    $userInfo =  sendRequest($url,[],[</span><br><span class="line">        <span class="string">"Accept: application/json"</span>,</span><br><span class="line">        <span class="string">"User-Agent: ilearn"</span>,  <span class="comment">// 此处(ilearn)是填写应用名称 或者 github用户名</span></span><br><span class="line">        <span class="string">"Authorization:token &#123;$access_token&#125;"</span></span><br><span class="line">    ]);</span><br><span class="line"></span><br><span class="line">    exit($userInfo);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果登陆失败就打印错误信息</span></span><br><span class="line">echo <span class="string">"&lt;p&gt;登陆失败&lt;/p&gt;&lt;/pre&gt;"</span>;</span><br><span class="line">var_dump($response);</span><br><span class="line">exit(<span class="string">"&lt;/pre&gt;"</span>);</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//github.com/overtrue/laravel-socialite</span></span><br><span class="line"></span><br><span class="line">使用命令 composer <span class="built_in">require</span> huoshaotuzi/sociate 安装，现在支持 QQ、微博、微信公众号、百度、Github 登录，以后有时间还会继续扩展更多的登录。https:<span class="comment">//learnku.com/laravel/t/24449</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-安全问题入门"><a href="#PHP-安全问题入门" class="headerlink" title="PHP 安全问题入门"></a>PHP 安全问题入门</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">SQL 注入</span><br><span class="line">$username = $_GET[<span class="string">'username'</span>];</span><br><span class="line">$query = $pdo-&gt;prepare(<span class="string">'SELECT * FROM users WHERE username = :username'</span>);</span><br><span class="line">$query-&gt;execute([<span class="string">'username'</span> =&gt; $username]);</span><br><span class="line">$data = $query-&gt;fetch();</span><br><span class="line"></span><br><span class="line">xss </span><br><span class="line">$searchQuery = htmlentities($searchQuery, ENT_QUOTES);</span><br><span class="line">XSRF/CSRF</span><br><span class="line">最常用的防御方法是生成一个 CSRF 令牌加密安全字符串，一般称其为 Token，并将 Token 存储于 Cookie 或者 Session 中。</span><br><span class="line"></span><br><span class="line">每次你在网页构造表单时，将 Token 令牌放在表单中的隐藏字段，表单请求服务器以后会根据用户的 Cookie 或者 Session 里的 Token 令牌比对，校验成功才给予通过</span><br><span class="line"></span><br><span class="line">密码哈希</span><br><span class="line"></span><br><span class="line"><span class="comment">//user signup</span></span><br><span class="line">$password = $_POST[<span class="string">'password'</span>];</span><br><span class="line">$hashedPassword = password_hash($password, PASSWORD_DEFAULT);</span><br><span class="line"></span><br><span class="line"><span class="comment">//login</span></span><br><span class="line">$password = $_POST[<span class="string">'password'</span>];</span><br><span class="line">$hash = <span class="string">'1234'</span>; <span class="comment">//load this value from your db</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(password_verify($password, $hash)) &#123;</span><br><span class="line">  echo <span class="string">'Password is valid!'</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  echo <span class="string">'Invalid password.'</span>;</span><br><span class="line">&#125;</span><br><span class="line">密码哈希并不是密码加密。哈希（Hash）是将目标文本转换成具有相同长度的、不可逆的杂凑字符串（或叫做消息摘要），而加密（Encrypt）是将目标文本转换成具有不同长度的、可逆的密文。显然他们之间最大的区别是可逆性，在储存密码时，我们要的就是哈希这种不可逆的属性。</span><br><span class="line"></span><br><span class="line">命令注入</span><br><span class="line">$targetIp = escapeshellarg($_GET[<span class="string">'ip'</span>]);</span><br><span class="line">$output = shell_exec(<span class="string">"ping -c 5 $targetIp"</span>);</span><br><span class="line"></span><br><span class="line">像登录这样的敏感表单应该有一个严格的速率限制，以防止暴力攻击。保存每个用户在过去几分钟内失败的登录尝试次数，如果该速率超过你定义的阈值，则拒绝进一步登录尝试，直到冷却期结束。还可通过电子邮件通知用户登录失败，以便他们知道自己的账户被成为目标。</span><br><span class="line">https:<span class="comment">//learnku.com/php/t/24930#reply94655</span></span><br></pre></td></tr></table></figure>
<h3 id="内置-Web-服务器"><a href="#内置-Web-服务器" class="headerlink" title="内置 Web 服务器"></a>内置 Web 服务器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">php -S localhost:<span class="number">8000</span> -t public/</span><br><span class="line">php -S localhost:<span class="number">8000</span> router.php</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 对URL进行解析，并获取请求的文件名</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">$uri = urldecode(parse_url($_SERVER[<span class="string">"REQUEST_URI"</span>], PHP_URL_PATH));</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是否存在该文件，如果不存在，则直接继续加载入口文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">if</span> ($uri !== <span class="string">"/"</span> &amp;&amp; file_exists(__DIR__ . <span class="string">"$uri"</span>)) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 加载入口文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">require_once <span class="string">"./index.php"</span>;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/28768#reply94663</span></span><br></pre></td></tr></table></figure>
<h3 id="phpexcel"><a href="#phpexcel" class="headerlink" title="phpexcel"></a>phpexcel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/PHPOffice/PhpSpreadsheet</span></span><br><span class="line"></span><br><span class="line">composer <span class="built_in">require</span> phpoffice/phpspreadsheet</span><br><span class="line"><span class="built_in">require</span> <span class="string">'vendor/autoload.php'</span>;</span><br><span class="line"></span><br><span class="line">use PhpOffice\PhpSpreadsheet\Spreadsheet;</span><br><span class="line">use PhpOffice\PhpSpreadsheet\Writer\Xlsx;</span><br><span class="line"></span><br><span class="line">$spreadsheet = <span class="keyword">new</span> Spreadsheet();</span><br><span class="line">$sheet = $spreadsheet-&gt;getActiveSheet();</span><br><span class="line">$sheet-&gt;setCellValue(<span class="string">'A1'</span>, <span class="string">'Hello World !'</span>);</span><br><span class="line"></span><br><span class="line">$writer = <span class="keyword">new</span> Xlsx($spreadsheet);</span><br><span class="line">$writer-&gt;save(<span class="string">'hello world.xlsx'</span>);</span><br><span class="line">php -S localhost:<span class="number">8000</span> -t vendor/phpoffice/phpspreadsheet/samples</span><br><span class="line"><span class="comment">// 文件路径</span></span><br><span class="line">$inputFileName = <span class="string">'./sampleData/example1.xls'</span>;</span><br><span class="line"></span><br><span class="line">$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);</span><br><span class="line">$reader = <span class="keyword">new</span> \PhpOffice\PhpSpreadsheet\Reader\Xls();</span><br><span class="line">只要读取数据，不要格式时，实例读取器中 readDataOnly 属性，如下</span><br><span class="line"></span><br><span class="line">$inputFileType = <span class="string">'Xls'</span>;</span><br><span class="line">$inputFileName = <span class="string">'./sampleData/example1.xls'</span>;</span><br><span class="line"></span><br><span class="line">$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);</span><br><span class="line"><span class="comment">/**  只要数据  **/</span></span><br><span class="line">$reader-&gt;setReadDataOnly(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">$spreadsheet = $reader-&gt;load($inputFileName);</span><br><span class="line"></span><br><span class="line">多个文件合并为一个对象</span><br><span class="line"></span><br><span class="line">$inputFileType = <span class="string">'Csv'</span>;</span><br><span class="line">$inputFileNames = [</span><br><span class="line">    <span class="string">'./sampleData/example1.csv'</span>,</span><br><span class="line">    <span class="string">'./sampleData/example2.csv'</span></span><br><span class="line">    <span class="string">'./sampleData/example3.csv'</span></span><br><span class="line">];</span><br><span class="line"></span><br><span class="line">$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);</span><br><span class="line"></span><br><span class="line"><span class="comment">/** 拿到第一个  **/</span></span><br><span class="line">$inputFileName = array_shift($inputFileNames);</span><br><span class="line"></span><br><span class="line">$spreadsheet = $reader-&gt;load($inputFileName);</span><br><span class="line"></span><br><span class="line">$spreadsheet-&gt;getActiveSheet()</span><br><span class="line">    -&gt;setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));</span><br><span class="line"></span><br><span class="line"><span class="comment">/**  循环读取  **/</span></span><br><span class="line">foreach($inputFileNames <span class="keyword">as</span> $sheet =&gt; $inputFileName) &#123;</span><br><span class="line">    <span class="comment">/**  重新设置工作表索引  **/</span></span><br><span class="line">    $reader-&gt;setSheetIndex($sheet+<span class="number">1</span>);</span><br><span class="line">    <span class="comment">/**  把文件当做一个新的工作表载入  **/</span></span><br><span class="line">    $reader-&gt;loadIntoExisting($inputFileName,$spreadsheet);</span><br><span class="line">    <span class="comment">/**  设置工作表标题  **/</span></span><br><span class="line">    $spreadsheet-&gt;getActiveSheet()</span><br><span class="line">        -&gt;setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$spreadsheet = PhpOffice\PhpSpreadsheet\IOFactory::load(<span class="string">"new.xls"</span>);</span><br><span class="line"></span><br><span class="line">$data = $spreadsheet</span><br><span class="line">            -&gt;getSheet(<span class="number">0</span>) <span class="comment">// 指定第一个工作表为当前</span></span><br><span class="line">            -&gt;toArray();  <span class="comment">// 转为数组</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 或者得到全部工作表的数据数组           </span></span><br><span class="line">$cells=array();</span><br><span class="line"><span class="comment">// 工作表对象有迭代器实现</span></span><br><span class="line">foreach ( $spreadsheet-&gt;getWorksheetIterator() <span class="keyword">as</span> $data ) &#123;</span><br><span class="line">    $cells = $data-&gt;toArray();</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30048</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-多进程"><a href="#PHP-多进程" class="headerlink" title="PHP 多进程"></a>PHP 多进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">$processNum = <span class="number">4</span>;</span><br><span class="line">$timeStart = time();</span><br><span class="line">$tasks = range(<span class="number">1</span>, <span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">$jobs = [];</span><br><span class="line">foreach ($tasks <span class="keyword">as</span> $task) &#123;</span><br><span class="line">    <span class="comment">//这里讲返回的结果对4进行取模，存入4个数组，然后4个进程分别读取不同的数据进行处理</span></span><br><span class="line">    $jobs[$task % $processNum][] = $task;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$mainPid = posix_getpid();</span><br><span class="line">echo <span class="string">"主进程："</span> . $mainPid . PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $processNum; $i++) &#123;</span><br><span class="line">    $pid = pcntl_fork();</span><br><span class="line">    <span class="keyword">if</span> ($pid == <span class="number">-1</span>) &#123;</span><br><span class="line">        <span class="comment">//错误处理：创建子进程失败时返回-1.</span></span><br><span class="line">        die(<span class="string">'could not fork'</span>);</span><br><span class="line">    &#125; elseif ($pid) &#123;</span><br><span class="line">        <span class="comment">//父进程会得到子进程号，所以这里是父进程执行的逻辑</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123; <span class="comment">// 子进程得到的$pid为0, 所以这里是子进程执行的逻辑。</span></span><br><span class="line">        $content = $jobs[$i];</span><br><span class="line">        $childStart = time();</span><br><span class="line">        foreach ($content <span class="keyword">as</span> $v2) &#123;</span><br><span class="line">            sleep(<span class="number">1</span>); <span class="comment">// 子进程执行的逻辑</span></span><br><span class="line">        &#125;</span><br><span class="line">        $childEnd = time();</span><br><span class="line">        $childDiff = $childEnd - $childStart;</span><br><span class="line">        echo <span class="string">"#"</span> . posix_getpid() . <span class="string">"执行完毕，用时："</span> . $childDiff . <span class="string">"秒"</span> . PHP_EOL;</span><br><span class="line">        exit(); <span class="comment">// 子进程执行完后必须退出，否则会循环的创建进程...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//这里挂起主进程，等待子进程全部退出后再退出主进程</span></span><br><span class="line"><span class="keyword">while</span> ($processNum &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> ( pcntl_wait($status) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        $processNum--;</span><br><span class="line">        echo <span class="string">"#"</span> . $pid . <span class="string">"退出"</span> . PHP_EOL;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$timeEnd = time();</span><br><span class="line">$diff = $timeEnd - $timeStart;</span><br><span class="line">echo <span class="string">'共计用时：'</span> . $diff . <span class="string">'秒'</span>;https:<span class="comment">//learnku.com/laravel/t/30134</span></span><br></pre></td></tr></table></figure>
<h3 id="php-进程"><a href="#php-进程" class="headerlink" title="php 进程"></a>php 进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">后台运行</span><br><span class="line"><span class="keyword">if</span> ($pid=pcntl_fork ()) exit (<span class="number">0</span>);<span class="comment">// 是父进程，结束父进程，子进程继续</span></span><br><span class="line">脱离控制终端，登录会话和进程组</span><br><span class="line">posix_setsid ();<span class="comment">// 子进程升级组长进程，脱离原来的会话 / 终端</span></span><br><span class="line">禁止进程重新打开控制终端</span><br><span class="line"><span class="keyword">if</span> ($pid=pcntl_fork ()) exit (<span class="number">0</span>);<span class="comment">// 结束第一子进程，第二子进程继续（第二子进程不再是会话组长）</span></span><br><span class="line">关闭打开的文件描述符</span><br><span class="line">fclose (STDIN),fclose (STDOUT),fclose (STDERR)<span class="comment">// 关闭标准输入输出与错误显示。</span></span><br><span class="line">改变当前工作目录</span><br><span class="line">chdir(<span class="string">"/"</span>)</span><br><span class="line">重设文件创建掩模</span><br><span class="line">umask (<span class="number">0</span>);<span class="comment">// 防止继承父级遗留下来的掩模</span></span><br></pre></td></tr></table></figure>
<h3 id="码模拟实现队列"><a href="#码模拟实现队列" class="headerlink" title="码模拟实现队列"></a>码模拟实现队列</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">define(<span class="string">'MAXSIZE'</span>,<span class="number">5</span>);<span class="comment">//队列空间大小</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @param $queue 队列存储空间</span></span><br><span class="line"><span class="comment"> * @param $front 队列队头游标</span></span><br><span class="line"><span class="comment"> * @param $near  队列队尾游标</span></span><br><span class="line"><span class="comment"> * @param $data  进队的数据</span></span><br><span class="line"><span class="comment"> * @return int</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">enQueue</span>(<span class="params">&amp;$queue,$front,$near,$data</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (($near+<span class="number">1</span>)%MAXSIZE==$front)&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"队空间已满"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    $queue[$near%MAXSIZE] = $data;</span><br><span class="line">    $near+=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> $near;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 出队操作</span></span><br><span class="line"><span class="comment"> * @param $queue</span></span><br><span class="line"><span class="comment"> * @param $front</span></span><br><span class="line"><span class="comment"> * @param $near</span></span><br><span class="line"><span class="comment"> * @return int</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">deQueue</span>(<span class="params">&amp;$queue,$front,$near</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($front==$near%MAXSIZE)&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"空队列"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    $data = $queue[$front];</span><br><span class="line">    echo <span class="string">"出队元素："</span>.$data.PHP_EOL;</span><br><span class="line">    $front = ($front+<span class="number">1</span>)%MAXSIZE;</span><br><span class="line">    <span class="keyword">return</span> $front;</span><br><span class="line">&#125;</span><br><span class="line">$a = [];<span class="comment">//队列 使用数组模拟</span></span><br><span class="line">$front = $near = <span class="number">0</span>;<span class="comment">//游标初始化</span></span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">1</span>);</span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">2</span>);</span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line"><span class="comment">//$front = deQueue($a,$front,$near);</span></span><br><span class="line"></span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">100</span>);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line"></span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">1</span>);</span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">2</span>);</span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">3</span>);</span><br><span class="line">$near = enQueue($a,$front,$near,<span class="number">4</span>);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line">$front = deQueue($a,$front,$near);</span><br><span class="line">队列实现了先进先出 FIFO 的功能，在许多应用中如队列系统，排队买票等都应用到了，比如我们用的 redis 缓存系统中的列数据类型 List，它便能完成队列的功能</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30430</span></span><br></pre></td></tr></table></figure>
<h3 id="夏令时"><a href="#夏令时" class="headerlink" title="夏令时"></a>夏令时</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//判断美国那个时间段是否为夏令时</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">is_dst</span>(<span class="params">$timestamp</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $timezone = date(<span class="string">'e'</span>); <span class="comment">//获取当前使用的时区</span></span><br><span class="line">    date_default_timezone_set(<span class="string">'US/Pacific-New'</span>); <span class="comment">//强制设置时区</span></span><br><span class="line">    $dst = date(<span class="string">'I'</span>,$timestamp); <span class="comment">//判断是否夏令时</span></span><br><span class="line">    date_default_timezone_set($timezone); <span class="comment">//还原时区</span></span><br><span class="line">    <span class="keyword">return</span> $dst; <span class="comment">//返回结果</span></span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//gist.github.com/flowerains/6694812</span></span><br><span class="line"><span class="comment">//获取当月第一天和最后一天</span></span><br><span class="line">  private <span class="function"><span class="keyword">function</span> <span class="title">getthemonth</span>(<span class="params">$date</span>) </span></span><br><span class="line"><span class="function">	</span>&#123; </span><br><span class="line">		$firstday = date(<span class="string">'Y-m-01'</span>, strtotime($date)); </span><br><span class="line">		$lastday = date(<span class="string">'Y-m-d'</span>, strtotime(<span class="string">"$firstday +1 month -1 day"</span>)); </span><br><span class="line">		<span class="keyword">return</span> array($firstday, $lastday); </span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h3 id="去除emoji"><a href="#去除emoji" class="headerlink" title="去除emoji"></a>去除emoji</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">removeEmoji</span>(<span class="params">$text</span>) </span>&#123;</span><br><span class="line">    $clean_text = <span class="string">""</span>;</span><br><span class="line">    <span class="comment">// Match Emoticons</span></span><br><span class="line">    $regexEmoticons = <span class="string">'/[\x&#123;1F600&#125;-\x&#123;1F64F&#125;]/u'</span>;</span><br><span class="line">    $clean_text = preg_replace($regexEmoticons, <span class="string">''</span>, $text);</span><br><span class="line">    <span class="comment">// Match Miscellaneous Symbols and Pictographs</span></span><br><span class="line">    $regexSymbols = <span class="string">'/[\x&#123;1F300&#125;-\x&#123;1F5FF&#125;]/u'</span>;</span><br><span class="line">    $clean_text = preg_replace($regexSymbols, <span class="string">''</span>, $clean_text);</span><br><span class="line">    <span class="comment">// Match Transport And Map Symbols</span></span><br><span class="line">    $regexTransport = <span class="string">'/[\x&#123;1F680&#125;-\x&#123;1F6FF&#125;]/u'</span>;</span><br><span class="line">    $clean_text = preg_replace($regexTransport, <span class="string">''</span>, $clean_text);</span><br><span class="line">    <span class="keyword">return</span> $clean_text;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="imagick等比缩放图片"><a href="#imagick等比缩放图片" class="headerlink" title="imagick等比缩放图片"></a>imagick等比缩放图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 使用imagick 等比缩放图片 </span></span><br><span class="line"><span class="comment">     * @param string $source_img  源图片地址 </span></span><br><span class="line"><span class="comment">     * @param string $target_img  缩放后图片地址 </span></span><br><span class="line"><span class="comment">     * @param int $with   缩放后图片宽度 </span></span><br><span class="line"><span class="comment">     * @param int $height  缩放后图片高度 </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">imagick</span>(<span class="params">$source_img,$target_img,$with,$height</span>)</span>&#123; </span><br><span class="line">        <span class="keyword">if</span>(is_file($source_img))&#123; <span class="comment">//判断源图片是否存在 </span></span><br><span class="line">            $im = <span class="keyword">new</span> Imagick(); </span><br><span class="line">        &#125;<span class="keyword">else</span>&#123; </span><br><span class="line">            exit; </span><br><span class="line">        &#125; </span><br><span class="line">        $result = $im-&gt;readImage($source_img);  </span><br><span class="line">        $srcWH = $im-&gt;getImageGeometry(); <span class="comment">//获取源图片宽和高 </span></span><br><span class="line">        <span class="comment">//图片等比例缩放宽和高设置 ，根据宽度设置等比缩放 </span></span><br><span class="line">        <span class="keyword">if</span>($srcWH[<span class="string">'width'</span>]&gt;$<span class="keyword">with</span>)&#123; </span><br><span class="line">            $srcW[<span class="string">'width'</span>] = $<span class="keyword">with</span>; </span><br><span class="line">            $srcH[<span class="string">'height'</span>] = $srcW[<span class="string">'width'</span>]/$srcWH[<span class="string">'width'</span>]*$srcWH[<span class="string">'height'</span>]; </span><br><span class="line">        &#125;<span class="keyword">else</span>&#123; </span><br><span class="line">            $srcW[<span class="string">'width'</span>] = $srcWH[<span class="string">'width'</span>]; </span><br><span class="line">            $srcH[<span class="string">'height'</span>] = $srcWH[<span class="string">'height'</span>]; </span><br><span class="line">        &#125; </span><br><span class="line">         </span><br><span class="line">        <span class="comment">//按照比例进行缩放 </span></span><br><span class="line">        $im-&gt;thumbnailImage( $srcW[<span class="string">'width'</span>], $srcH[<span class="string">'height'</span>], <span class="literal">true</span> );  </span><br><span class="line">         </span><br><span class="line">        <span class="comment">// 按照缩略图大小创建一个有颜色的图片 </span></span><br><span class="line">        $new_img= <span class="keyword">new</span> Imagick(); </span><br><span class="line">        $new_img-&gt;newImage( $srcW[<span class="string">'width'</span>], $srcH[<span class="string">'height'</span>], <span class="string">'white'</span>, <span class="string">'jpg'</span> ); <span class="comment">//pink,black </span></span><br><span class="line">         </span><br><span class="line">        <span class="comment">//合并图片 </span></span><br><span class="line">        $new_img-&gt;compositeImage( $im, <span class="attr">imagick</span>::COMPOSITE_OVER, <span class="number">0</span>, <span class="number">0</span>);  </span><br><span class="line">        <span class="comment">//生成图片 </span></span><br><span class="line">        $new_img-&gt;setImageFileName($target_img); </span><br><span class="line">        $new_img-&gt;writeImage(); </span><br><span class="line">        <span class="comment">//输出图片 </span></span><br><span class="line">        header( <span class="string">"Content-Type: image/jpg"</span> ); </span><br><span class="line">        echo $new_img; </span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br><span class="line">https:<span class="comment">//gist.github.com/flowerains/beaab90b1bfc01054de3</span></span><br></pre></td></tr></table></figure>
<h3 id="redis支持php的序列化和反序列化"><a href="#redis支持php的序列化和反序列化" class="headerlink" title="redis支持php的序列化和反序列化"></a>redis支持php的序列化和反序列化</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$redis = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>, <span class="number">1</span>);</span><br><span class="line">$redis-&gt;setOption(Redis::OPT_SERIALIZER, <span class="attr">Redis</span>::SERIALIZER_PHP);</span><br><span class="line">$key = <span class="string">'this_is_test'</span>;</span><br><span class="line">$value = [<span class="string">'15'</span>,<span class="string">'12'</span>,<span class="number">15</span>,<span class="string">'alex'</span>];</span><br><span class="line">$name = $redis-&gt;rPush($key,$value);</span><br><span class="line">$result = $redis-&gt;lpop($key);</span><br><span class="line">var_dump($result);</span><br></pre></td></tr></table></figure>
<h3 id="一致性hash"><a href="#一致性hash" class="headerlink" title="一致性hash"></a>一致性hash</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 整数hash</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">intHash</span>(<span class="params">$key</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $md5 = substr(md5($key), <span class="number">0</span>, <span class="number">8</span>);</span><br><span class="line">    $seed = <span class="number">31</span>;</span><br><span class="line">    $hash = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">0</span>;$i &lt; <span class="number">8</span>; ++$i) &#123;</span><br><span class="line">        $hash = $hash * $seed + ord(md5($i));</span><br><span class="line">        ++$i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $hash &amp; <span class="number">0x7FFFFFFF</span>;</span><br><span class="line">&#125;</span><br><span class="line">$value = intHash(<span class="string">'wangchuang'</span>) % <span class="number">2</span>;</span><br><span class="line">echo $value;</span><br></pre></td></tr></table></figure>
<h3 id="抽奖的经典算法"><a href="#抽奖的经典算法" class="headerlink" title="抽奖的经典算法"></a>抽奖的经典算法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_rand</span>(<span class="params">$proArr</span>) </span>&#123; </span><br><span class="line">    $result = <span class="string">''</span>; </span><br><span class="line"> </span><br><span class="line">    <span class="comment">//概率数组的总概率精度 </span></span><br><span class="line">    $proSum = array_sum($proArr); </span><br><span class="line"> </span><br><span class="line">    <span class="comment">//概率数组循环 </span></span><br><span class="line">    foreach ($proArr <span class="keyword">as</span> $key =&gt; $proCur) &#123; </span><br><span class="line">        $randNum = mt_rand(<span class="number">1</span>, $proSum); </span><br><span class="line">        <span class="keyword">if</span> ($randNum &lt;= $proCur) &#123; </span><br><span class="line">            $result = $key; </span><br><span class="line">            <span class="keyword">break</span>; </span><br><span class="line">        &#125; <span class="keyword">else</span> &#123; </span><br><span class="line">            $proSum -= $proCur; </span><br><span class="line">        &#125; </span><br><span class="line">    &#125; </span><br><span class="line">    unset ($proArr); </span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> $result; </span><br><span class="line">&#125; </span><br><span class="line">$prize_arr = array( </span><br><span class="line">    <span class="string">'0'</span> =&gt; array(<span class="string">'id'</span>=&gt;<span class="number">1</span>,<span class="string">'prize'</span>=&gt;<span class="string">'平板电脑'</span>,<span class="string">'v'</span>=&gt;<span class="number">1</span>), </span><br><span class="line">    <span class="string">'1'</span> =&gt; array(<span class="string">'id'</span>=&gt;<span class="number">2</span>,<span class="string">'prize'</span>=&gt;<span class="string">'数码相机'</span>,<span class="string">'v'</span>=&gt;<span class="number">5</span>), </span><br><span class="line">    <span class="string">'2'</span> =&gt; array(<span class="string">'id'</span>=&gt;<span class="number">3</span>,<span class="string">'prize'</span>=&gt;<span class="string">'音箱设备'</span>,<span class="string">'v'</span>=&gt;<span class="number">10</span>), </span><br><span class="line">    <span class="string">'3'</span> =&gt; array(<span class="string">'id'</span>=&gt;<span class="number">4</span>,<span class="string">'prize'</span>=&gt;<span class="string">'4G优盘'</span>,<span class="string">'v'</span>=&gt;<span class="number">12</span>), </span><br><span class="line">    <span class="string">'4'</span> =&gt; array(<span class="string">'id'</span>=&gt;<span class="number">5</span>,<span class="string">'prize'</span>=&gt;<span class="string">'10Q币'</span>,<span class="string">'v'</span>=&gt;<span class="number">22</span>), </span><br><span class="line">    <span class="string">'5'</span> =&gt; array(<span class="string">'id'</span>=&gt;<span class="number">6</span>,<span class="string">'prize'</span>=&gt;<span class="string">'下次没准就能中哦'</span>,<span class="string">'v'</span>=&gt;<span class="number">50</span>), </span><br><span class="line">); </span><br><span class="line">foreach ($prize_arr <span class="keyword">as</span> $key =&gt; $val) &#123; </span><br><span class="line">    $arr[$val[<span class="string">'id'</span>]] = $val[<span class="string">'v'</span>]; </span><br><span class="line">&#125; </span><br><span class="line"> </span><br><span class="line">$rid = get_rand($arr); <span class="comment">//根据概率获取奖项id </span></span><br><span class="line"> </span><br><span class="line">$res[<span class="string">'yes'</span>] = $prize_arr[$rid<span class="number">-1</span>][<span class="string">'prize'</span>]; <span class="comment">//中奖项 </span></span><br><span class="line">unset($prize_arr[$rid<span class="number">-1</span>]); <span class="comment">//将中奖项从数组中剔除，剩下未中奖项 </span></span><br><span class="line">shuffle($prize_arr); <span class="comment">//打乱数组顺序 </span></span><br><span class="line"><span class="keyword">for</span>($i=<span class="number">0</span>;$i&lt;count($prize_arr);$i++)&#123; </span><br><span class="line">    $pr[] = $prize_arr[$i][<span class="string">'prize'</span>]; </span><br><span class="line">&#125; </span><br><span class="line">$res[<span class="string">'no'</span>] = $pr; </span><br><span class="line">echo json_encode($res); </span><br><span class="line">https:<span class="comment">//gist.github.com/flowerains/1e2e57906d496206a097</span></span><br><span class="line"></span><br><span class="line">$proArr = [<span class="string">'1'</span>=&gt;<span class="number">65</span>,<span class="string">'2'</span>=&gt;<span class="number">20</span>,<span class="string">'3'</span>=&gt;<span class="number">5</span>,<span class="string">'4'</span>=&gt;<span class="number">5</span>,<span class="string">'5'</span>=&gt;<span class="number">5</span>];</span><br><span class="line">$rank = get_rand($proArr);</span><br><span class="line">$i = <span class="number">0</span>;</span><br><span class="line"><span class="comment">//统计次数</span></span><br><span class="line">$num1 = <span class="number">0</span>;</span><br><span class="line">$num2 = <span class="number">0</span>;</span><br><span class="line">$num3 = <span class="number">0</span>;</span><br><span class="line">$num4 = <span class="number">0</span>;</span><br><span class="line">$num5 = <span class="number">0</span>;</span><br><span class="line">$arr = array();</span><br><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line">    $rank = get_rand($proArr);</span><br><span class="line">    <span class="keyword">switch</span> ($rank) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">            $num1++;</span><br><span class="line">            <span class="keyword">break</span>; </span><br><span class="line">        <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">            $num2++;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">            $num3++;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">            $num4++;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">            $num5++;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    $arr[] = $rank;</span><br><span class="line">    $i++;</span><br><span class="line">&#125;<span class="keyword">while</span>($i &lt; <span class="number">100000</span>);</span><br><span class="line">$value1 = number_format($num1/count($arr),<span class="number">4</span>)*<span class="number">100</span>;</span><br><span class="line">$value2 = number_format($num2/count($arr),<span class="number">4</span>)*<span class="number">100</span>;</span><br><span class="line">$value3 = number_format($num3/count($arr),<span class="number">4</span>)*<span class="number">100</span>;</span><br><span class="line">$value4 = number_format($num4/count($arr),<span class="number">4</span>)*<span class="number">100</span>;</span><br><span class="line">$value5 = number_format($num5/count($arr),<span class="number">4</span>)*<span class="number">100</span>;</span><br><span class="line">echo <span class="string">"1的中奖率%$value1\n"</span>;</span><br><span class="line">echo <span class="string">"2的中奖率%$value2\n"</span>;</span><br><span class="line">echo <span class="string">"3的中奖率%$value3\n"</span>;</span><br><span class="line">echo <span class="string">"4的中奖率%$value4\n"</span>;</span><br><span class="line">echo <span class="string">"5的中奖率%$value5\n"</span>;</span><br></pre></td></tr></table></figure>
<h3 id="本月第一天"><a href="#本月第一天" class="headerlink" title="本月第一天"></a>本月第一天</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//上一周的周一周末 </span></span><br><span class="line">$start=date(<span class="string">"Y-m-d"</span>,strtotime(<span class="string">"2016-11-07"</span>.<span class="string">"-1 week Monday"</span>));</span><br><span class="line">echo date(<span class="string">"Y-m-d"</span>,strtotime(<span class="string">"$start +6 day"</span>));</span><br><span class="line"><span class="comment">//下一周的周一周末</span></span><br><span class="line">$start=date(<span class="string">"Y-m-d"</span>,strtotime(<span class="string">"2016-11-07"</span>.<span class="string">"+1 week Monday"</span>));   </span><br><span class="line">echo date(<span class="string">"Y-m-d"</span>,strtotime(<span class="string">"$start +6 day"</span>));  </span><br><span class="line"><span class="comment">//本月的第一天，最后一天    </span></span><br><span class="line">$start=date(<span class="string">'Y-m-01'</span>, strtotime(date(<span class="string">"Y-m-d"</span>)));   </span><br><span class="line">echo date(<span class="string">'Y-m-d'</span>, strtotime(<span class="string">"$start +1 month -1 day"</span>));</span><br></pre></td></tr></table></figure>
<h3 id="多维数组转一维数组"><a href="#多维数组转一维数组" class="headerlink" title="多维数组转一维数组"></a>多维数组转一维数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">$a=array(</span><br><span class="line">    <span class="number">0</span>=&gt;array(</span><br><span class="line">      <span class="number">0</span>=&gt;<span class="string">"a"</span>,</span><br><span class="line">        <span class="number">1</span>=&gt;array(</span><br><span class="line">          <span class="number">0</span>=&gt;<span class="string">'b'</span></span><br><span class="line">        )</span><br><span class="line">    ),</span><br><span class="line">    <span class="number">1</span>=&gt;array(</span><br><span class="line">      <span class="number">0</span>=&gt;<span class="string">'c'</span>,</span><br><span class="line">        <span class="number">1</span>=&gt;<span class="string">'d'</span>,</span><br><span class="line">        <span class="number">2</span>=&gt;array(</span><br><span class="line">          <span class="number">0</span>=&gt;array(</span><br><span class="line">           <span class="string">"ddd"</span>=&gt;array(</span><br><span class="line">              <span class="number">0</span>=&gt;<span class="string">'e'</span>,</span><br><span class="line">               <span class="string">"a"</span>=&gt;<span class="string">'f'</span>,</span><br><span class="line">               <span class="number">1</span>=&gt;<span class="string">'g'</span></span><br><span class="line">            )</span><br><span class="line">          )</span><br><span class="line">        )</span><br><span class="line">    ),</span><br><span class="line">    <span class="string">'w'</span>=&gt;<span class="string">'h'</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">//获取未知维度数组最里面的值，换成一维数组https://3v4l.org/Mrjr1</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_array_value</span>(<span class="params">$data</span>)</span>&#123;</span><br><span class="line">  $result=array();</span><br><span class="line">  array_walk_recursive($data,<span class="function"><span class="keyword">function</span>(<span class="params">$value</span>) <span class="title">use</span> (<span class="params">&amp;$result</span>)</span>&#123;</span><br><span class="line">    array_push($result,$value);</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">return</span> $result;</span><br><span class="line">&#125;</span><br><span class="line">print_r(get_array_value($a));</span><br><span class="line"><span class="built_in">Array</span></span><br><span class="line">(</span><br><span class="line">    [<span class="number">0</span>] =&gt; a</span><br><span class="line">    [<span class="number">1</span>] =&gt; b</span><br><span class="line">    [<span class="number">2</span>] =&gt; c</span><br><span class="line">    [<span class="number">3</span>] =&gt; d</span><br><span class="line">    [<span class="number">4</span>] =&gt; e</span><br><span class="line">    [<span class="number">5</span>] =&gt; f</span><br><span class="line">    [<span class="number">6</span>] =&gt; g</span><br><span class="line">    [<span class="number">7</span>] =&gt; h</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<h3 id="面向对象的三大特性"><a href="#面向对象的三大特性" class="headerlink" title="面向对象的三大特性"></a>面向对象的三大特性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br></pre></td><td class="code"><pre><span class="line">封装，继承，多态 面向对象的五大原则</span><br><span class="line">         </span><br><span class="line">         单一职责原则，开放封闭原则，里氏替换原则，依赖倒置原则，接口隔离原则</span><br><span class="line">         </span><br><span class="line">         </span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 输出一个字符串</span></span><br><span class="line"><span class="comment"> * 装饰器动态添加功能</span></span><br><span class="line"><span class="comment"> * Class EchoText</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EchoText</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $decorator = [];</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">Index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//调用装饰器前置操作</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;beforeEcho();</span><br><span class="line">        echo <span class="string">"你好，我是装饰器。"</span>;</span><br><span class="line">        <span class="comment">//调用装饰器后置操作</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;afterEcho();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//增加装饰器</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">addDecorator</span>(<span class="params">Decorator $decorator</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;decorator[] = $decorator;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//执行装饰器前置操作 先进先出原则</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">beforeEcho</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;decorator <span class="keyword">as</span> $decorator)</span><br><span class="line">            $decorator-&gt;before();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//执行装饰器后置操作 先进后出原则</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">afterEcho</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $tmp = array_reverse($<span class="keyword">this</span>-&gt;decorator);</span><br><span class="line">        foreach ($tmp <span class="keyword">as</span> $decorator)</span><br><span class="line">            $decorator-&gt;after();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 装饰器接口</span></span><br><span class="line"><span class="comment"> * Class Decorator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">interface Decorator</span><br><span class="line">&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    <span class="title">public</span> <span class="title">function</span> <span class="title">after</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">/**</span></span><br><span class="line"><span class="function"> * 颜色装饰器实现</span></span><br><span class="line"><span class="function"> * <span class="title">Class</span> <span class="title">ColorDecorator</span></span></span><br><span class="line"><span class="function"> */</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">ColorDecorator</span> <span class="title">implements</span> <span class="title">Decorator</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    protected $color;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$color</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;color = $color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;dis style='color: &#123;$this-&gt;color&#125;'&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">after</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;/div&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 字体大小装饰器实现</span></span><br><span class="line"><span class="comment"> * Class SizeDecorator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SizeDecorator</span> <span class="title">implements</span> <span class="title">Decorator</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $size;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$size</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;size = $size;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;dis style='font-size: &#123;$this-&gt;size&#125;px'&gt;**"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">after</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;/div&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//实例化输出类</span></span><br><span class="line">$echo = <span class="keyword">new</span> EchoText();</span><br><span class="line"><span class="comment">//增加装饰器</span></span><br><span class="line">$echo-&gt;addDecorator(<span class="keyword">new</span> ColorDecorator(<span class="string">'red'</span>));</span><br><span class="line"><span class="comment">//增加装饰器</span></span><br><span class="line">$echo-&gt;addDecorator(<span class="keyword">new</span> SizeDecorator(<span class="string">'22'</span>));</span><br><span class="line"><span class="comment">//输出</span></span><br><span class="line">$echo-&gt;Index();</span><br><span class="line">interface employee</span><br><span class="line">&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">working</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">teacher</span> <span class="title">implements</span> <span class="title">employee</span>//具体应该依赖与抽象</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">working</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'teaching...'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">coder</span> <span class="title">implements</span> <span class="title">employee</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">working</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'coding...'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">workA</span>//例子1</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $teacher = <span class="keyword">new</span> teacher;</span><br><span class="line">        $teacher-&gt;working();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">workB</span>//例子2</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $e;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">set</span>(<span class="params">employee $e</span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;e = $e;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;e-&gt;working();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$worka = <span class="keyword">new</span> workA;<span class="comment">//workA 依赖于 teacher 类 不符合依赖倒置原则</span></span><br><span class="line">$worka-&gt;work();</span><br><span class="line">$workb = <span class="keyword">new</span> workB;<span class="comment">//workB 不依赖与某个类 既可以注入 teacher 也可以 注入 coder</span></span><br><span class="line">$workb-&gt;set(<span class="keyword">new</span> teacher());</span><br><span class="line">$workb-&gt;work();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30034#267039</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-并发"><a href="#PHP-并发" class="headerlink" title="PHP 并发"></a>PHP 并发</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">使用队列，额外起一个进程处理队列，并发请求都放到队列中，由额外进程串行处理，并发问题就不存在了，但是要额外进程支持以及处理延迟严重，本文不先不讨论这种方法。</span><br><span class="line">利用数据库事务特征，做原子更新，此方法需要依赖数据库的事务特性。</span><br><span class="line">借助文件排他锁，在处理下单请求的时候，用 flock 锁定一个文件，成功拿到锁的才能处理订单</span><br><span class="line">&lt;?php</span><br><span class="line">$http = <span class="keyword">new</span> swoole_http_server(<span class="string">"0.0.0.0"</span>, <span class="number">9509</span>);   <span class="comment">// 监听 9509</span></span><br><span class="line"></span><br><span class="line">$http-&gt;set(array(</span><br><span class="line">    <span class="string">'reactor_num'</span> =&gt; <span class="number">2</span>,  <span class="comment">//reactor thread num</span></span><br><span class="line">    <span class="string">'worker_num'</span> =&gt; <span class="number">4</span>    <span class="comment">//worker process num</span></span><br><span class="line">));</span><br><span class="line"></span><br><span class="line">$http-&gt;on(<span class="string">'request'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">swoole_http_request $request, swoole_http_response $response</span>) </span>&#123;</span><br><span class="line">    $uniqid = uniqid(<span class="string">'uid-'</span>, TRUE);    <span class="comment">// 模拟唯一用户ID</span></span><br><span class="line">    $redis = <span class="keyword">new</span> Redis();</span><br><span class="line">    $redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);    <span class="comment">// 连接 redis</span></span><br><span class="line"></span><br><span class="line">    $redis-&gt;watch(<span class="string">'rest_count'</span>);  <span class="comment">// 监测 rest_count 是否被其它的进程更改</span></span><br><span class="line"></span><br><span class="line">    $rest_count = intval($redis-&gt;get(<span class="string">"rest_count"</span>));  <span class="comment">// 模拟唯一订单ID</span></span><br><span class="line">    <span class="keyword">if</span> ($rest_count &gt; <span class="number">0</span>)&#123;</span><br><span class="line">        $value = <span class="string">"&#123;$rest_count&#125;-&#123;$uniqid&#125;"</span>;  <span class="comment">// 表示当前订单，被当前用户抢到了</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// do something ... 主要是模拟用户抢到单后可能要进行的一些密集运算</span></span><br><span class="line">        $rand  = rand(<span class="number">100</span>, <span class="number">1000000</span>);</span><br><span class="line">        $sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $rand; $i++) &#123;$sum += $i;&#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// redis 事务</span></span><br><span class="line">        $redis-&gt;multi();</span><br><span class="line">        $redis-&gt;lPush(<span class="string">'uniqids'</span>, $value);</span><br><span class="line">        $redis-&gt;decr(<span class="string">'rest_count'</span>);</span><br><span class="line">        $replies = $redis-&gt;exec();  <span class="comment">// 执行以上 redis 事务</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">// 如果 rest_count 的值被其它的并发进程更改了，以上事务将回滚</span></span><br><span class="line">        <span class="keyword">if</span> (!$replies) &#123;</span><br><span class="line">            echo <span class="string">"订单 &#123;$value&#125; 回滚"</span> . PHP_EOL;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    $redis-&gt;unwatch();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$http-&gt;start();</span><br><span class="line">使用 ab 测试https:<span class="comment">//learnku.com/articles/29686</span></span><br><span class="line"></span><br><span class="line">$ ab -t <span class="number">20</span> -c <span class="number">10</span> http:<span class="comment">//192.168.1.104:9509/</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-守护程序"><a href="#PHP-守护程序" class="headerlink" title="PHP 守护程序"></a>PHP 守护程序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 编写一个简单daemon程序https://learnku.com/articles/30595</span></span><br><span class="line">$pid = pcntl_fork();</span><br><span class="line"></span><br><span class="line"><span class="keyword">switch</span>($pid) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">0</span>: </span><br><span class="line">            <span class="comment">// 2.子进程逻辑</span></span><br><span class="line">            $mypid = posix_getpid();</span><br><span class="line">            <span class="comment">//echo "Son pid is $mypid\n";</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 3.升级组长进程</span></span><br><span class="line">            <span class="keyword">if</span> (!$sid = posix_setsid()) &#123;</span><br><span class="line">                    exit(<span class="string">"Set sid error\n"</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 4.防止组长进程再次控制终端</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="number">-1</span> == pcntl_fork()) &#123;</span><br><span class="line">                    exit(<span class="string">"Fork error\n"</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 5.标准输入/输出/错误</span></span><br><span class="line">            fclose(STDIN);</span><br><span class="line">            fclose(STDOUT);</span><br><span class="line">            fclose(STDERR);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 6.改变目录</span></span><br><span class="line">            chdir(<span class="string">"/"</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 7.重设掩码</span></span><br><span class="line">            umask(<span class="number">0</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">case</span> <span class="number">-1</span>:</span><br><span class="line">            <span class="comment">// fork err</span></span><br><span class="line">            exit(<span class="string">"Fork error\n"</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="comment">// 1.父进程逻辑</span></span><br><span class="line">            $mypid = posix_getpid();</span><br><span class="line">            <span class="comment">//echo "Parent pid is $mypid\n";</span></span><br><span class="line">            exit();</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; <span class="number">10000</span>; $i ++) &#123;</span><br><span class="line">        file_put_contents(<span class="string">"/tmp/wutao.log"</span>, <span class="string">"i=$i\n"</span>, FILE_APPEND);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">sleep(<span class="number">10</span>);</span><br></pre></td></tr></table></figure>
<h3 id="JSON-encode-精度缺失"><a href="#JSON-encode-精度缺失" class="headerlink" title="JSON_encode () 精度缺失"></a>JSON_encode () 精度缺失</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">  $info = <span class="number">26.54</span>;</span><br><span class="line">    $res = json_encode($info);</span><br><span class="line">    var_dump($res);</span><br><span class="line">运行结果</span><br><span class="line"></span><br><span class="line">string(<span class="number">18</span>) <span class="string">"26.539999999999999"</span></span><br><span class="line"></span><br><span class="line">serialize_precision = <span class="number">16</span></span><br><span class="line">&gt;&gt;&gt; ini_get(<span class="string">'serialize_precision'</span>)</span><br><span class="line">=&gt; <span class="string">"17"</span></span><br><span class="line">&gt;&gt;&gt; ini_set(<span class="string">'serialize_precision'</span>,<span class="number">16</span>)</span><br><span class="line">=&gt; <span class="string">"17"</span></span><br><span class="line">&gt;&gt;&gt; json_encode($info);</span><br><span class="line">=&gt; <span class="string">"26.54"</span></span><br></pre></td></tr></table></figure>
<h3 id="生成器"><a href="#生成器" class="headerlink" title="生成器"></a>生成器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRange</span> (<span class="params">$max = <span class="number">10</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt; $max; $i++) &#123;</span><br><span class="line">        $injected = <span class="keyword">yield</span> $i;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($injected === <span class="string">'stop'</span>) <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$generator = getRange(PHP_INT_MAX);</span><br><span class="line">传递参数到生成器中</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">foreach ($generator <span class="keyword">as</span> $range) &#123;</span><br><span class="line">    <span class="keyword">if</span> ($range === <span class="number">10000</span>) &#123;</span><br><span class="line">        $generator-&gt;send(<span class="string">'stop'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    echo <span class="string">"Dataset &#123;$range&#125; &lt;br&gt;"</span>;</span><br><span class="line">&#125;</span><br><span class="line">注意: 在生成器中使用 <span class="keyword">return</span> ，会跳出生成器。</span><br><span class="line">https:<span class="comment">//learnku.com/php/t/28336</span></span><br></pre></td></tr></table></figure>
<h3 id="字符串分割"><a href="#字符串分割" class="headerlink" title="字符串分割"></a>字符串分割</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">$str = <span class="string">'#本科 &amp; 硕士 @博士 - 教授'</span>;</span><br><span class="line">        $limit1 = array(<span class="string">'#'</span>, <span class="string">'&amp;'</span>, <span class="string">'@'</span>, <span class="string">'-'</span>);</span><br><span class="line">        $limit2 = array(<span class="string">'$#$'</span>, <span class="string">'$&amp;$'</span>, <span class="string">'$@$'</span>, <span class="string">'$-$'</span>);</span><br><span class="line">        $str2 = str_replace($limit1, $limit2, $str);</span><br><span class="line">        $arr = array();</span><br><span class="line">        $arr1 = array_values(array_filter(explode(<span class="string">'$'</span>, $str2)));</span><br><span class="line">        foreach ($arr1 <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($k % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                $arr[] = array($v, $arr1[$k + <span class="number">1</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        print_r($arr);      https:<span class="comment">//learnku.com/index.php/laravel/t/30777</span></span><br><span class="line">      $string = <span class="string">'#本科 &amp; 硕士 @博士 - 教授'</span>;</span><br><span class="line">      $matches = [];</span><br><span class="line">      preg_match_all(<span class="string">'/([#|&amp;|@|\-])([^#|&amp;|@|\-]*)/'</span>, $string, $matches);</span><br><span class="line">      </span><br><span class="line">      $signArr = array_map(<span class="string">'trim'</span>, $matches[<span class="number">1</span>]);</span><br><span class="line">      $titleArr = array_map(<span class="string">'trim'</span>, $matches[<span class="number">2</span>]);</span><br><span class="line">      # 在这以下的代码你可以自由发挥了</span><br><span class="line">      $data = [];</span><br><span class="line">      $minCount = min(count($signArr), $titleArr);</span><br><span class="line">      <span class="keyword">for</span> ($i=<span class="number">0</span>; $i&lt;$minCount; $i++)</span><br><span class="line">      &#123;</span><br><span class="line">          $data[] = [</span><br><span class="line">              $signArr[$i],</span><br><span class="line">              $titleArr[$i],</span><br><span class="line">          ];</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>
<h3 id="闭包和匿名函数"><a href="#闭包和匿名函数" class="headerlink" title="闭包和匿名函数"></a>闭包和匿名函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">callFunc1</span>(<span class="params">Closure $closure</span>) </span>&#123;</span><br><span class="line">    $closure();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">callFunc2</span>(<span class="params">Callable $callback</span>) </span>&#123;</span><br><span class="line">    $callback();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">xy</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo <span class="string">'Hello, World!'</span>;</span><br><span class="line">&#125;</span><br><span class="line">$<span class="function"><span class="keyword">function</span> = <span class="title">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo <span class="string">'Hello, World!'</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">callFunc1(<span class="string">"xy"</span>); <span class="comment">// Catchable fatal error: Argument 1 passed to callFunc1() must be an instance of Closure, string given</span></span><br><span class="line">callFunc2(<span class="string">"xy"</span>); <span class="comment">// Hello, World!</span></span><br><span class="line"></span><br><span class="line">callFunc1($<span class="function"><span class="keyword">function</span>); // <span class="title">Hello</span>, <span class="title">World</span>!</span></span><br><span class="line"><span class="function"><span class="title">callFunc2</span>(<span class="params">$function</span>); // <span class="title">Hello</span>, <span class="title">World</span>!</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">//从<span class="title">php7</span>.1开始可以使用以下代码转换</span></span><br><span class="line"><span class="function"><span class="title">callFunc1</span>(<span class="params">Closure::fromCallable(<span class="string">"xy"</span></span>))</span></span><br><span class="line"><span class="function">其中 <span class="title">callable</span> 参数可以为</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">is_callable</span>(<span class="params"><span class="string">'functionName'</span></span>); </span></span><br><span class="line"><span class="function"><span class="title">is_callable</span>(<span class="params">[$foo, <span class="string">'bar'</span>]</span>);//<span class="title">$foo</span>-&gt;<span class="title">bar</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function"><span class="title">is_callable</span>(<span class="params">[<span class="string">'foo'</span>,<span class="string">'bar'</span>]</span>);//<span class="title">foo</span>::<span class="title">bar</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function"><span class="title">is_callable</span>(<span class="params">function(</span>)</span>&#123;&#125;);<span class="comment">//closure</span></span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">subscribeSellOrder</span>(<span class="params">SellOrderProcess $sellOrderProcess</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  $closure = self::makeClosure($sellOrderProcess, <span class="string">'sellOrdersProcess'</span>);</span><br><span class="line">  $<span class="keyword">this</span>-&gt;redis-&gt;subscribe(RtdataConst::SELL_CHANNEL_NAME, $closure);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">protected <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">makeClosure</span>(<span class="params">$obj, $method</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> Closure::fromCallable([$obj, $method]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/index.php/articles/30767</span></span><br></pre></td></tr></table></figure>
<h3 id="curl绑定host"><a href="#curl绑定host" class="headerlink" title="curl绑定host"></a>curl绑定host</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">xxx.cn 测试的host为<span class="number">172.16</span><span class="number">.252</span><span class="number">.2</span></span><br><span class="line">$url = <span class="string">"http://172.16.252.2/api/test"</span>;</span><br><span class="line"></span><br><span class="line">$client = <span class="keyword">new</span> Client([<span class="string">'emitter'</span> =&gt; <span class="keyword">new</span> \App\Services\GuzzleEmitter(<span class="string">'test'</span>)]);</span><br><span class="line">$response = $client-&gt;get($url,array(<span class="string">"timeout"</span>=&gt;<span class="number">3</span>,<span class="string">"connect_timeout"</span>=&gt;<span class="number">2</span>,<span class="string">'headers'</span>=&gt;[<span class="string">'host'</span>=&gt;<span class="string">'xxx.cn'</span>]));</span><br></pre></td></tr></table></figure>
<h3 id="错误和异常"><a href="#错误和异常" class="headerlink" title="错误和异常"></a>错误和异常</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/articles/25813</span></span><br><span class="line">错误并不能被 <span class="keyword">try</span> <span class="keyword">catch</span> 捕捉</span><br><span class="line">Parse error 解析错误   Fatal error 致命错误  Warning 警告  Notice 注意</span><br><span class="line">error_report 设置错误的报告级别error_reporting(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">set_error_handler 设置用户自定义的错误处理函数</span><br><span class="line">set_error_handler(<span class="string">'myErrorFun'</span>); <span class="comment">//把php原始的错误处理机制，变成我们的myErrorFun函数处理</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myErrorFun</span>(<span class="params">$errno, $message, $file, $line</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    echo <span class="string">'错误码是：'</span>.$errno.<span class="string">'&lt;/br&gt;'</span>;</span><br><span class="line">    echo <span class="string">'错误的信息是'</span>.$message.<span class="string">'&lt;/br&gt;'</span>;</span><br><span class="line">    echo <span class="string">'错误的文件是：'</span>.$file.<span class="string">'&lt;/br&gt;'</span>;</span><br><span class="line">    echo <span class="string">'错误的行数是'</span>.$line;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">echo $a; <span class="comment">//a是未定义的变量</span></span><br><span class="line">错误是由于 php 脚本自身的问题导致的和逻辑无关，异常则是由于逻辑问题导致的</span><br><span class="line">在 php7 之前错误不能用 <span class="keyword">try</span> <span class="keyword">catch</span> 捕获，异常可以 </span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"大江东去，浪淘尽，千古风流人物"</span>); <span class="comment">//抛出异常</span></span><br><span class="line">异常必须要手动抛出才行否则捕获不到 手动抛出异常是 php 比较鸡肋的地方，异常必须要手动抛出，才能捕获到，但是既然已经知道哪里会发生异常，直接规避不就可以了吗？或者重写逻辑，这可能是 php 比较特殊的地方吧</span><br><span class="line">&#125; <span class="keyword">catch</span> (Exception $e) &#123; <span class="comment">//系统内置的异常处理类</span></span><br><span class="line">    echo $e-&gt;getMessage(); <span class="comment">//获取异常信息</span></span><br><span class="line">&#125;</span><br><span class="line">自定义的异常处理函数，用来处理没有用 <span class="keyword">try</span> <span class="keyword">catch</span> 捕获的异常，如果你抛出的异常无人捕获，那么就会进入该方法 </span><br><span class="line">set_exception_handler(<span class="string">'myexception'</span>);</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myexception</span>(<span class="params">$exception</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    var_dump($exception-&gt;getMessage());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"测试一下自定义异常处理函"</span>);</span><br></pre></td></tr></table></figure>
<h3 id="装饰器"><a href="#装饰器" class="headerlink" title="装饰器"></a>装饰器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line">封装，继承，多态</span><br><span class="line"></span><br><span class="line">什么是封装？</span><br><span class="line"></span><br><span class="line">把客观的事物封装成抽象的类，并且类可以把自己的数据和方法只让可信的类或者对象操作，对不可信的类进行信息的隐藏。简单的说就是：封装使对象的设计者与对象的使用者分开，使用者只要知道对象可以做什么就可以了，不需要知道具体是怎么实现的。封装可以有助于提高类和系统的安全性。</span><br><span class="line"></span><br><span class="line">什么是继承？</span><br><span class="line"></span><br><span class="line">继承指的是建立一个新的派生类，从一个或多个先前定义的类中继承数据和函数，可以重新定义或加进新数据和函数，从而建立了类的层次或等级。</span><br><span class="line"></span><br><span class="line">什么是多态？</span><br><span class="line"></span><br><span class="line">多态性指的是： 同一操作作用与不同类的实例，将产生不同的执行结果，即不同类的对象收到相同的消息时，将得到不同的结果。</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 输出一个字符串</span></span><br><span class="line"><span class="comment"> * 装饰器动态添加功能</span></span><br><span class="line"><span class="comment"> * Class EchoText</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EchoText</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $decorator = [];</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">Index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//调用装饰器前置操作</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;beforeEcho();</span><br><span class="line">        echo <span class="string">"你好，我是装饰器。"</span>;</span><br><span class="line">        <span class="comment">//调用装饰器后置操作</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;afterEcho();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//增加装饰器</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">addDecorator</span>(<span class="params">Decorator $decorator</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;decorator[] = $decorator;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//执行装饰器前置操作 先进先出原则</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">beforeEcho</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;decorator <span class="keyword">as</span> $decorator)</span><br><span class="line">            $decorator-&gt;before();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//执行装饰器后置操作 先进后出原则</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">afterEcho</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $tmp = array_reverse($<span class="keyword">this</span>-&gt;decorator);</span><br><span class="line">        foreach ($tmp <span class="keyword">as</span> $decorator)</span><br><span class="line">            $decorator-&gt;after();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 装饰器接口</span></span><br><span class="line"><span class="comment"> * Class Decorator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">interface Decorator</span><br><span class="line">&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    <span class="title">public</span> <span class="title">function</span> <span class="title">after</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">/**</span></span><br><span class="line"><span class="function"> * 颜色装饰器实现</span></span><br><span class="line"><span class="function"> * <span class="title">Class</span> <span class="title">ColorDecorator</span></span></span><br><span class="line"><span class="function"> */</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">ColorDecorator</span> <span class="title">implements</span> <span class="title">Decorator</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    protected $color;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$color</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;color = $color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;dis style='color: &#123;$this-&gt;color&#125;'&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">after</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;/div&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 字体大小装饰器实现</span></span><br><span class="line"><span class="comment"> * Class SizeDecorator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SizeDecorator</span> <span class="title">implements</span> <span class="title">Decorator</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $size;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$size</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;size = $size;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;dis style='font-size: &#123;$this-&gt;size&#125;px'&gt;**"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">after</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"&lt;/div&gt;"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//实例化输出类</span></span><br><span class="line">$echo = <span class="keyword">new</span> EchoText();</span><br><span class="line"><span class="comment">//增加装饰器</span></span><br><span class="line">$echo-&gt;addDecorator(<span class="keyword">new</span> ColorDecorator(<span class="string">'red'</span>));</span><br><span class="line"><span class="comment">//增加装饰器</span></span><br><span class="line">$echo-&gt;addDecorator(<span class="keyword">new</span> SizeDecorator(<span class="string">'22'</span>));</span><br><span class="line"><span class="comment">//输出https://learnku.com/articles/30034</span></span><br><span class="line">$echo-&gt;Index();</span><br></pre></td></tr></table></figure>
<h3 id="理解-cookie、session、token"><a href="#理解-cookie、session、token" class="headerlink" title="理解 cookie、session、token"></a>理解 cookie、session、token</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">session这对服务器说是一个巨大的开销 ， 严重的限制了服务器扩展能力， 比如说我用两个机器组成了一个集群， 小 F 通过机器 A 登录了系统， 那 session id 会保存在机器 A 上， 假设小 F 的下一次请求被转发到机器 B 怎么办？机器 B 可没有小 F 的 session id 啊。有时候会采用一点小伎俩：session sticky ， 就是让小 F 的请求一直粘连在机器 A 上， 但是这也不管用， 要是机器 A 挂掉了， 还得转到机器 B 去。那只好做 session 的复制了， 把 session id 在两个机器之间搬来搬去， 快累死了。</span><br><span class="line">小 F 已经登录了系统， 我给他发一个令牌 token， 里边包含了小 F 的 user id， 下一次小 F 再次通过 Http 请求访问我的时候， 把这个 token 通过 Http header 带过来不就可以了。*</span><br><span class="line"></span><br><span class="line">不过这和 session id 没有本质区别啊， 任何人都可以可以伪造， 所以我得想点儿办法， 让别人伪造不了。</span><br><span class="line"></span><br><span class="line">那就对数据做一个签名吧， 比如说我用 HMAC-SHA256 算法，加上一个只有我才知道的密钥， 对数据做一个签名， 把这个签名和数据一起作为 token， 由于密钥别人不知道， 就无法伪造 token 了。</span><br><span class="line"></span><br><span class="line">基于 Token 的身份验证的过程如下:</span><br><span class="line"></span><br><span class="line">用户通过用户名和密码发送请求。</span><br><span class="line">程序验证。</span><br><span class="line">程序返回一个签名的 token 给客户端。</span><br><span class="line">客户端储存 token, 并且每次用于每次发送请求。</span><br><span class="line">服务端验证 token 并返回数据。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30051</span></span><br></pre></td></tr></table></figure>
<h3 id="分割字符串"><a href="#分割字符串" class="headerlink" title="分割字符串"></a>分割字符串</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mb_str_split</span>(<span class="params">$string</span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_string($string)) &#123;</span><br><span class="line">            <span class="keyword">return</span> preg_split(<span class="string">'/(?&lt;!^)(?!$)/u'</span>, $string);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> [];</span><br><span class="line">    &#125;</span><br><span class="line">    &gt;&gt;&gt; mb_str_split(<span class="string">'我是谁'</span>)</span><br><span class="line">    =&gt; [</span><br><span class="line">         <span class="string">"我"</span>,</span><br><span class="line">         <span class="string">"是"</span>,</span><br><span class="line">         <span class="string">"谁"</span>,</span><br><span class="line">       ]</span><br></pre></td></tr></table></figure>
<h3 id="计算身份证的最后一位验证码"><a href="#计算身份证的最后一位验证码" class="headerlink" title="计算身份证的最后一位验证码"></a>计算身份证的最后一位验证码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @functionName 计算身份证的最后一位验证码</span></span><br><span class="line"><span class="comment">     * @description  根据国家标准GB 11643-1999，前提必须是18位的证件号</span></span><br><span class="line"><span class="comment">     *https://github.com/alicfeng/identity-card</span></span><br><span class="line"><span class="comment">     * @param string $idBody 证件号码的前17位数字</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return bool|mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">calculateCode</span>(<span class="params">$idBody</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">17</span> != strlen($idBody)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//加权因子</span></span><br><span class="line">        $factor = [<span class="number">7</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">4</span>, <span class="number">2</span>];</span><br><span class="line">        <span class="comment">//校验码对应值</span></span><br><span class="line">        $code     = [<span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'X'</span>, <span class="string">'9'</span>, <span class="string">'8'</span>, <span class="string">'7'</span>, <span class="string">'6'</span>, <span class="string">'5'</span>, <span class="string">'4'</span>, <span class="string">'3'</span>, <span class="string">'2'</span>];</span><br><span class="line">        $checksum = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        foreach (range(<span class="number">0</span>, strlen($idBody) - <span class="number">1</span>) <span class="keyword">as</span> $index =&gt; $item) &#123;</span><br><span class="line">            $checksum += substr($idBody, $index, <span class="number">1</span>) * $factor[$index];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $code[$checksum % <span class="number">11</span>];</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="简单工厂模式"><a href="#简单工厂模式" class="headerlink" title="简单工厂模式"></a>简单工厂模式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line">使用简单工厂模式重新实现达到解耦的目的</span><br><span class="line"></span><br><span class="line">将加减乘除解耦，每一个运算用单独的类去写，再用工厂模式实例化出合适的对象，通过多态返回计算器结果，后面如果再加一个平方根运算可以新增一个平方根的类，如果需要修改加法运算，那就只需要修改 OperationAdd 类。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Operation</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $num1;</span><br><span class="line">    public $num2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OperationAdd</span> <span class="keyword">extends</span> <span class="title">Operation</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getResult</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = $<span class="keyword">this</span>-&gt;num1 + $<span class="keyword">this</span>-&gt;num2;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OperationSub</span> <span class="keyword">extends</span> <span class="title">Operation</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getResult</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = $<span class="keyword">this</span>-&gt;num1 - $<span class="keyword">this</span>-&gt;num2;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OperationMul</span> <span class="keyword">extends</span> <span class="title">Operation</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getResult</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = $<span class="keyword">this</span>-&gt;num1 * $<span class="keyword">this</span>-&gt;num2;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OperationDiv</span> <span class="keyword">extends</span> <span class="title">Operation</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getResult</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;num2 != <span class="number">0</span>) &#123;</span><br><span class="line">            $result = $<span class="keyword">this</span>-&gt;num1 / $<span class="keyword">this</span>-&gt;num2;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $result = <span class="string">"除数不能为0"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OperationFactory</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">createOperate</span>(<span class="params">$symbol</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> ($symbol) &#123;<span class="comment">//多态</span></span><br><span class="line">            <span class="keyword">case</span> <span class="string">'+'</span>:</span><br><span class="line">                $result = <span class="keyword">new</span> OperationAdd();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'-'</span>:</span><br><span class="line">                $result = <span class="keyword">new</span> OperationSub();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'*'</span>:</span><br><span class="line">                $result = <span class="keyword">new</span> OperationMul();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'/'</span>:</span><br><span class="line">                $result = <span class="keyword">new</span> OperationDiv();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                $result = NULL;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$operationFactory = OperationFactory::createOperate(<span class="string">'+'</span>);</span><br><span class="line">$operationFactory-&gt;num1 = <span class="number">21</span>;</span><br><span class="line">$operationFactory-&gt;num2 = <span class="number">11</span>;</span><br><span class="line"></span><br><span class="line">echo $operationFactory-&gt;getResult();<span class="comment">//32  https://learnku.com/articles/32491</span></span><br></pre></td></tr></table></figure>
<h3 id="array-reduce-多值化一"><a href="#array-reduce-多值化一" class="headerlink" title="array_reduce 多值化一"></a>array_reduce 多值化一</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">array_reduce 函数内部实现机制，更类似于如下代码</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">array_reduce2</span>(<span class="params">$array, $callback, $initial=null</span>)</span>&#123;</span><br><span class="line">        $acc = $initial;</span><br><span class="line">        foreach($array <span class="keyword">as</span> $a)</span><br><span class="line">            $acc = $callback($acc, $a);</span><br><span class="line">        <span class="keyword">return</span> $acc;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">   array_reduce2([<span class="number">2</span>,<span class="number">4</span>],<span class="function"><span class="keyword">function</span>(<span class="params">$carry,$x</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>  $carry+$x;</span><br><span class="line">    &#125;);  <span class="comment">// 6</span></span><br><span class="line">    将一个序列函数的嵌套调用组合为 array_reduce 产生的新函数调用</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">compose</span>(<span class="params">...$functions</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> array_reduce(</span><br><span class="line">            $functions,</span><br><span class="line">            <span class="function"><span class="keyword">function</span>(<span class="params">$carry,$function</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">$x</span>)<span class="title">use</span>(<span class="params">$carry,$function</span>)</span>&#123;</span><br><span class="line">                    <span class="keyword">return</span> $<span class="function"><span class="keyword">function</span>(<span class="params">$carry($x</span>));</span></span><br><span class="line"><span class="function">                &#125;;</span></span><br><span class="line"><span class="function">            &#125;,</span></span><br><span class="line"><span class="function">            <span class="title">function</span>(<span class="params">$x</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> $x;</span><br><span class="line">            &#125;</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">    效果</span><br><span class="line">    $compose = compose(</span><br><span class="line">        <span class="comment">// 加 2</span></span><br><span class="line">        <span class="function"><span class="keyword">function</span> (<span class="params">$x</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $x + <span class="number">2</span>;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 乘 4</span></span><br><span class="line">        <span class="function"><span class="keyword">function</span> (<span class="params">$x</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $x * <span class="number">4</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    );</span><br><span class="line">    </span><br><span class="line">    $res = $compose(<span class="number">3</span>); <span class="comment">// （3+2）*4 = 20  https://learnku.com/articles/32417</span></span><br></pre></td></tr></table></figure>
<h3 id="过滤空白字符"><a href="#过滤空白字符" class="headerlink" title="过滤空白字符"></a>过滤空白字符</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">$s=<span class="string">"　　空白字符"</span>;</span><br><span class="line">&gt;&gt;&gt; strlen($s)</span><br><span class="line">=&gt; <span class="number">18</span></span><br><span class="line">说明前<span class="number">2</span>个字符长度为<span class="number">6</span>，是全角空格</span><br><span class="line">&gt;&gt;&gt; json_encode([<span class="string">'　'</span>])</span><br><span class="line">=&gt; <span class="string">"["</span>\u3000<span class="string">"]"</span></span><br><span class="line">&gt;&gt;&gt; preg_replace(<span class="string">"/\s+/u"</span>, <span class="string">""</span>,$s)</span><br><span class="line">=&gt; <span class="string">"空白字符"</span></span><br><span class="line">&gt;&gt;&gt; preg_replace(<span class="string">"/\s+/"</span>, <span class="string">""</span>,$s)</span><br><span class="line">=&gt; <span class="string">"　　空白字符"</span></span><br><span class="line">&gt;&gt;&gt; preg_replace(<span class="string">"/\x&#123;3000&#125;+/u"</span>,<span class="string">''</span>,$s)</span><br><span class="line">=&gt; <span class="string">"空白字符"</span></span><br><span class="line">&gt;&gt;&gt; trim($s,<span class="string">'　'</span>)</span><br><span class="line">=&gt; <span class="string">"空白字符"</span></span><br></pre></td></tr></table></figure>
<h3 id="curl-上传图片"><a href="#curl-上传图片" class="headerlink" title="curl 上传图片"></a>curl 上传图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;form action=<span class="string">"http://myServerURL"</span> method=<span class="string">"POST"</span> enctype=<span class="string">"multipart/form-data"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"file"</span> name=<span class="string">"file"</span> /&gt;</span><br><span class="line">    &lt;input type=<span class="string">"Submit"</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/form&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$fields = [</span></span><br><span class="line"><span class="regexp">    'image' =&gt; new \CurlFile(realpath($_FILES['img']['tmp_name']) )</span></span><br><span class="line"><span class="regexp">];</span></span><br><span class="line"><span class="regexp">curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);</span></span><br><span class="line"><span class="regexp">https:/</span><span class="regexp">/stackoverflow.com/</span>questions/<span class="number">21905942</span>/posting-raw-image-data-<span class="keyword">as</span>-multipart-form-data-<span class="keyword">in</span>-curl</span><br><span class="line">https:<span class="comment">//www.cnblogs.com/vanwee/p/10341298.html</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">upload</span>(<span class="params">$file, $multipart = true</span>) </span>&#123;</span><br><span class="line">    $cookie = <span class="string">''</span>;    <span class="comment">// 微博cookie</span></span><br><span class="line">    $url = <span class="string">'http://picupload.service.weibo.com/interface/pic_upload.php'</span></span><br><span class="line">    .<span class="string">'?mime=image%2Fjpeg&amp;data=base64&amp;url=0&amp;markpos=1&amp;logo=&amp;nick=0&amp;marks=1&amp;app=miniblog'</span>;</span><br><span class="line">    <span class="keyword">if</span>($multipart) &#123;</span><br><span class="line">        $url .= <span class="string">'&amp;cb=http://weibo.com/aj/static/upimgback.html?_wv=5&amp;callback=STK_ijax_'</span>.time();</span><br><span class="line">        <span class="keyword">if</span> (class_exists(<span class="string">'CURLFile'</span>)) &#123;     <span class="comment">// php 5.5</span></span><br><span class="line">            $post[<span class="string">'pic1'</span>] = <span class="keyword">new</span> CURLFile(realpath($file));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $post[<span class="string">'pic1'</span>] = <span class="string">'@'</span>.realpath($file);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        $post[<span class="string">'b64_data'</span>] = base64_encode(file_get_contents($file));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Curl提交 https://cloud.tencent.com/developer/article/1147512</span></span><br><span class="line">    $ch = curl_init($url);</span><br><span class="line">    curl_setopt_array($ch, array(</span><br><span class="line">        CURLOPT_POST =&gt; <span class="literal">true</span>,</span><br><span class="line">        CURLOPT_VERBOSE =&gt; <span class="literal">true</span>,</span><br><span class="line">        CURLOPT_RETURNTRANSFER =&gt; <span class="literal">true</span>,</span><br><span class="line">        CURLOPT_HTTPHEADER =&gt; array(<span class="string">"Cookie: $cookie"</span>),</span><br><span class="line">        CURLOPT_POSTFIELDS =&gt; $post,</span><br><span class="line">    ));</span><br><span class="line">    $output = curl_exec($ch);</span><br><span class="line">    curl_close($ch);</span><br><span class="line">    <span class="comment">// 正则表达式提取返回结果中的json数据</span></span><br><span class="line">    preg_match(<span class="string">'/(&#123;.*)/i'</span>, $output, $match);</span><br><span class="line">    <span class="keyword">if</span>(!isset($match[<span class="number">1</span>])) <span class="keyword">return</span> <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">return</span> $match[<span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line">upload(<span class="string">'mypic.jpg'</span>, <span class="literal">true</span>);  <span class="comment">// multipart方式上传</span></span><br><span class="line">upload(<span class="string">'http://www.mysite.cn/mypic.jpg'</span>, <span class="literal">false</span>);  <span class="comment">// 非multipart方式(base64)上传</span></span><br></pre></td></tr></table></figure>
<h3 id="快手短视频无水印"><a href="#快手短视频无水印" class="headerlink" title="快手短视频无水印"></a>快手短视频无水印</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> guzzlehttp/guzzle</span><br><span class="line">https:<span class="comment">//github.com/leixiaokou/kuaishou</span></span><br><span class="line"><span class="built_in">require</span> __DIR__.<span class="string">'/vendor/autoload.php'</span>;</span><br><span class="line">$url = $_GET[<span class="string">'url'</span>] ?? <span class="string">''</span>;</span><br><span class="line">$headers = [</span><br><span class="line">  <span class="string">'Connection'</span> =&gt; <span class="string">'keep-alive'</span>,</span><br><span class="line">  <span class="string">'User-Agent'</span>=&gt;<span class="string">'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1'</span></span><br><span class="line">];</span><br><span class="line">$client = <span class="keyword">new</span> \GuzzleHttp\Client([<span class="string">'timeout'</span> =&gt; <span class="number">2</span>, <span class="string">'headers'</span> =&gt; $headers, <span class="string">'http_errors'</span> =&gt; <span class="literal">false</span>,]);</span><br><span class="line">$data[<span class="string">'headers'</span>] = $headers;</span><br><span class="line">$jar = <span class="keyword">new</span> \GuzzleHttp\Cookie\CookieJar;</span><br><span class="line">$data[<span class="string">'cookies'</span>] = $jar;</span><br><span class="line">$response = $client-&gt;request(<span class="string">'GET'</span>, $url, $data);</span><br><span class="line"></span><br><span class="line">$body = $response-&gt;getBody();</span><br><span class="line"><span class="keyword">if</span> ($body <span class="keyword">instanceof</span> \GuzzleHttp\Psr7\Stream) &#123;</span><br><span class="line">  $body = $body-&gt;getContents();</span><br><span class="line">&#125;</span><br><span class="line">$result = htmlspecialchars_decode($body);</span><br><span class="line">$pattern = <span class="string">'#"srcNoMark":"(.*?)"#'</span>;</span><br><span class="line">preg_match($pattern, $result, $match);</span><br><span class="line">$data = [</span><br><span class="line">  <span class="string">'video_src'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">  <span class="string">'cover_image'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">];</span><br><span class="line"><span class="keyword">if</span> (empty($match[<span class="number">1</span>])) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line">$data[<span class="string">'video_src'</span>] = $match[<span class="number">1</span>];</span><br><span class="line">$pattern = <span class="string">'#"poster":"(.*?)"#'</span>;</span><br><span class="line">preg_match($pattern, $result, $match);</span><br><span class="line"><span class="keyword">if</span> (!empty($match[<span class="number">1</span>])) &#123;</span><br><span class="line">  $data[<span class="string">'cover_image'</span>] = $match[<span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line">echo json_encode($data);</span><br><span class="line">简单易用的获取无水印短视频播放地址，比如抖音无水印，快手无水印，微视无水印接口服务</span><br><span class="line">https:<span class="comment">//v.taokeduo.cn/api?uid=应用uid&amp;url=短视频平台的分享链接</span></span><br><span class="line">例如：https:<span class="comment">//v.taokeduo.cn/api?uid=06217e0cdb27d0200e3a4690f8907fd2&amp;url=https://v.douyin.com/vc4bUC/</span></span><br></pre></td></tr></table></figure>
<h3 id="根据另外一个数组去重"><a href="#根据另外一个数组去重" class="headerlink" title="根据另外一个数组去重"></a>根据另外一个数组去重</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">uniqueArray</span>(<span class="params">$arr, $plugins</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!$arr)&#123;</span><br><span class="line">            <span class="keyword">return</span> [];</span><br><span class="line">        &#125;</span><br><span class="line">        $tmp = [];<span class="comment">//相同的mid放一起</span></span><br><span class="line">        foreach ($arr <span class="keyword">as</span> $key =&gt; $v) &#123;</span><br><span class="line">            <span class="keyword">if</span> (isset($tmp[$v[<span class="string">'mid'</span>]])) &#123;</span><br><span class="line">                $tmp[$v[<span class="string">'mid'</span>]][] = $v;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $tmp[$v[<span class="string">'mid'</span>]] = [$v];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        $data = [];</span><br><span class="line">        foreach ($tmp <span class="keyword">as</span> $key =&gt; &amp;$value) &#123;</span><br><span class="line">            $has = <span class="number">0</span>;</span><br><span class="line">            foreach ($value <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">                <span class="keyword">if</span> ($v[<span class="string">'is_top'</span>] == <span class="number">1</span>) &#123;<span class="comment">//置顶的优先取</span></span><br><span class="line">                    $data[] = $v;</span><br><span class="line">                    $has = <span class="number">1</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!$has) &#123;</span><br><span class="line">                foreach ($value <span class="keyword">as</span> $k =&gt; &amp;$v) &#123;</span><br><span class="line">                    $v[<span class="string">'sort'</span>] = array_search($v[<span class="string">'plugin'</span>], $plugins);<span class="comment">//获取在另外一个数组的顺序</span></span><br><span class="line">                &#125;</span><br><span class="line">                $value =  arraySort($value,<span class="string">'sort'</span>,SORT_ASC);</span><br><span class="line">                $data[] = $value[<span class="number">0</span>];<span class="comment">//获取去重后的第一个</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $data;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">arraySort</span>(<span class="params">$array, $key, $sort = SORT_DESC</span>) </span>&#123;</span><br><span class="line">            $keysValue = [];</span><br><span class="line">            foreach ($array <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">                $keysValue[$k] = $v[$key];</span><br><span class="line">            &#125;</span><br><span class="line">            array_multisort($keysValue, $sort, $array);</span><br><span class="line">            <span class="keyword">return</span> $array;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取字符串内的链接"><a href="#获取字符串内的链接" class="headerlink" title="获取字符串内的链接"></a>获取字符串内的链接</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">preg_match_all(<span class="string">'/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i'</span>,strip_tags($content),$m);</span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/29685" target="_blank" rel="noopener">TCP/IP 协议</a></p>
<p><a href="https://learnku.com/articles/30341" target="_blank" rel="noopener">PHP 设计模式</a></p>
<p><a href="https://learnku.com/articles/25813" target="_blank" rel="noopener">php错误和异常</a></p>
<p><a href="https://learnku.com/articles/22942" target="_blank" rel="noopener">PHP 操作 Redis </a></p>
<p><a href="https://learnku.com/articles/24802" target="_blank" rel="noopener">Redis 基础</a></p>
<p><a href="https://github.com/wujunze/php-geo" target="_blank" rel="noopener">PHP地理位置</a></p>
<p><a href="https://learnku.com/articles/28114" target="_blank" rel="noopener">用 PHP 写一个微波炉</a></p>
<p><a href="https://github.com/mylxsw/wizard" target="_blank" rel="noopener">Laravel 开发的一款文档管理系统</a></p>
<p><a href="https://learnku.com/articles/30178" target="_blank" rel="noopener">PHP 开发量化交易的工具，模拟交易所 API 数据</a></p>
<p><a href="https://www.cnblogs.com/zyf-zhaoyafei/p/6928149.html" target="_blank" rel="noopener">再谈PHP错误与异常处理</a></p>
<p><a href="https://learnku.com/articles/30049" target="_blank" rel="noopener">MemCached 编译安装部署</a></p>
<p><a href="https://learnku.com/articles/30032" target="_blank" rel="noopener">TCP 的连接建立与关闭状态及数据传输通信过程</a></p>
<p><a href="">PhpSpreadsheet 小教程https://github.com/PHPOffice/PhpSpreadsheet</a></p>
<p><a href="https://learnku.com/articles/30051" target="_blank" rel="noopener">理解 cookie、session、token</a></p>
<p><a href="https://learnku.com/articles/29160" target="_blank" rel="noopener">如何快速接入 GitHub 登陆</a></p>
<p><a href="https://github.com/stabunkow/china-stock-data" target="_blank" rel="noopener">获取国内财经门户网股票数据composer包https://learnku.com/laravel/t/30040</a></p>
<p><a href="https://learnku.com/articles/30034" target="_blank" rel="noopener">面向对象的三大特性</a></p>
<p><a href="https://learnku.com/articles/29920" target="_blank" rel="noopener">PHP 经典面试问题解决过程：上台阶问题</a></p>
<p><a href="https://learnku.com/articles/29967" target="_blank" rel="noopener">Tideways、xhprof 和 xhgui 打造 PHP 非侵入式监控平台</a></p>
<p><a href="https://github.com/jc91715/oc-plugin-book" target="_blank" rel="noopener">文档协作翻译插件</a></p>
<p><a href="https://learnku.com/articles/29808" target="_blank" rel="noopener"> Slim 框架使用orm</a></p>
<p><a href="https://learnku.com/php/t/28934" target="_blank" rel="noopener">PHP 基础算题之动态规划leetcode</a></p>
<p><a href="https://learnku.com/articles/25760#replies" target="_blank" rel="noopener"> PC 端微信扫码支付全过程 — easywechat + Laravel 5.8</a></p>
<p><a href="https://free-andy.github.io/php-dict/" target="_blank" rel="noopener">php函数列表</a></p>
<p><a href="https://learnku.com/php/t/28922" target="_blank" rel="noopener">PHP 面向对象设计的五个基准原则</a></p>
<p><a href="https://learnku.com/articles/26995" target="_blank" rel="noopener">扫二维码登录过程</a></p>
<p><a href="https://learnku.com/php/t/26050" target="_blank" rel="noopener">使用 TUS 协议来实现大文件的断点续传</a></p>
<p><a href="https://github.com/terrylinooo/shieldon" target="_blank" rel="noopener">防爬虫机器人盗文</a></p>
<p><a href="https://zhaobugs.com/2018/12/04/Nginx%E4%B9%8B%E6%97%85-%E7%8E%AF%E5%A2%83%E7%A1%AE%E8%AE%A4%E5%8F%8ANginx%E7%9A%84%E5%AE%89%E8%A3%85/" target="_blank" rel="noopener">Nginx之旅-环境确认及Nginx的安装</a></p>
<p><a href="https://github.com/aoxiang594/laravel-province-city-area" target="_blank" rel="noopener">省市县数据扩展包</a></p>
<p><a href="https://learnku.com/articles/26686" target="_blank" rel="noopener">代理与反向代理、负载均衡和缓存</a></p>
<p><a href="https://wi1dcard.cn/posts/phpbrew-documention/" target="_blank" rel="noopener">多版本 PHP 共存phpbrew</a></p>
<p><a href="https://github.com/wi1dcard/thumb-php" target="_blank" rel="noopener">单文件 PHP 缩略图库</a></p>
<p><a href="https://www.smi1e.top/%E5%91%BD%E4%BB%A4%E6%B3%A8%E5%85%A5%E7%BB%95%E8%BF%87%E5%A7%BF%E5%8A%BF/" target="_blank" rel="noopener">命令注入绕过姿势</a></p>
<p><a href="https://coffeephp.com/" target="_blank" rel="noopener">PHP论坛</a></p>
<p><a href="http://blog.lanyus.com/archives/317.html" target="_blank" rel="noopener">搭建jetbrains激活服务器</a></p>
<p><a href="https://iluoy.com/articles/87" target="_blank" rel="noopener">phpstorm的奇技淫巧http://phpstorm.tips/tips</a></p>
<p><a href="http://www.phppan.com/php-design-pattern/" target="_blank" rel="noopener">PHP设计模式</a></p>
<p><a href="https://www.kancloud.cn/martist/ma_zhao_liu/376759" target="_blank" rel="noopener">开发者知识体系</a></p>
<p><a href="https://learnku.com/articles/10885/full-use-of-jwt" target="_blank" rel="noopener">JWT 完整使用详解</a></p>
<p><a href="http://www.edbiji.com/doccenter/showdoc/4/nav/678.html" target="_blank" rel="noopener">PHP笔记</a></p>
<p><a href="https://www.restran.net/2016/09/26/php-security-notes/" target="_blank" rel="noopener">CTF 中的 PHP 知识汇总</a></p>
<p><a href="https://yq.aliyun.com/articles/699892?spm=a2c4e.11153959.0.0.1a265ee3ENEDVQ" target="_blank" rel="noopener">聊聊服务稳定性保障这些事</a></p>
<p><a href="https://www.paycats.cn/" target="_blank" rel="noopener">个人开发者在线收款的服务</a></p>
<p><a href="https://learnku.com/articles/19465" target="_blank" rel="noopener">加密货币市场实时价格脚本</a></p>
<p><a href="https://github.com/chrismaltby/gb-studio" target="_blank" rel="noopener">GameBoy开发</a></p>
<p><a href="https://github.com/Danack/Imagick-demos" target="_blank" rel="noopener">An example of all the Imagick functions</a></p>
<p><a href="https://www.jishuwen.com/d/2NGy#tuit" target="_blank" rel="noopener">静态扫描为你的PHP项目上线保驾护航</a></p>
<p><a href="https://www.slidestalk.com/s/code_static_analysis" target="_blank" rel="noopener">静态扫描为你的PHP项目上线保驾护航</a></p>
<p><a href="https://yq.aliyun.com/teams/405/type_blog?spm=a2c4e.11153940.0.0.355e573f6aNW7x" target="_blank" rel="noopener">PHP技术进阶</a></p>
<p><a href="https://learnku.com/php/t/27412" target="_blank" rel="noopener">2019 PHP 安全指南</a></p>
<p><a href="https://laravel-china.github.io/php-the-right-way/" target="_blank" rel="noopener">php之道</a></p>
<p><a href="https://www.boatsky.com/blog/53" target="_blank" rel="noopener">2核1G内存的服务器能承载多少人访问</a></p>
<p><a href="https://www.boatsky.com/blog/26" target="_blank" rel="noopener">深入理解IEEE754的64位双精度</a></p>
<p><a href="https://andongshen.com/2019/04/21/webhooks-php/" target="_blank" rel="noopener">自动拉取github上的仓库</a></p>
<p><a href="https://github.com/mingyoung/dingtalk" target="_blank" rel="noopener">EasyDingTalk</a></p>
<p><a href="https://github.com/icecho/leetcode" target="_blank" rel="noopener">尝试 leetcode 题目</a></p>
<p><a href="https://www.qingwei.tech/programe-develops/php/622.html" target="_blank" rel="noopener">PHP常用函数整理</a></p>
<p><a href="https://github.com/alicfeng/note.samego.com" target="_blank" rel="noopener">笔记分享 | 简书2GitHub</a></p>
<p><a href="https://github.com/dedemao/weixinPay" target="_blank" rel="noopener">微信支付单文件版</a></p>
<p><a href="https://learnku.com/articles/28256" target="_blank" rel="noopener">无限级分类、多级菜单、省份城市</a></p>
<p><a href="https://www.fanhaobai.com/2018/09/process-php-multiprocess-server.html" target="_blank" rel="noopener">用PHP玩转进程之二 — 多进程PHPServer</a></p>
<p><a href="https://gitee.com/zmzmzm/unid" target="_blank" rel="noopener">unid是一个可以生成唯一ID的php扩展</a></p>
<p><a href="https://learnku.com/articles/28245#topnav" target="_blank" rel="noopener">秒杀系统的设计</a></p>
<p><a href="https://spacevim.org/cn/use-vim-as-a-php-ide/" target="_blank" rel="noopener">使用 Vim 搭建 PHP 开发环境</a></p>
<p><a href="https://github.com/free-andy/docker-dev" target="_blank" rel="noopener">docker 构建 php 开发环境</a></p>
<p><a href="https://github.com/maximebf/php-debugbar" target="_blank" rel="noopener">Debug bar for PHP</a></p>
<p><a href="https://github.com/FireLustre/php-dfa-sensitive" target="_blank" rel="noopener">过滤敏感词汇</a></p>
<p><a href="https://shuwoom.com/?p=3151" target="_blank" rel="noopener">COOKIE和SESSION机制详解</a></p>
<p><a href="https://www.njphper.com/posts/210095c.html" target="_blank" rel="noopener">webupload 实现分片上传视频</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/php/" rel="tag"># php</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/05/09/百度网盘资源/" rel="next" title="百度网盘资源">
                <i class="fa fa-chevron-left"></i> 百度网盘资源
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/05/13/github-精选项目收集/" rel="prev" title="github 精选项目收集">
                github 精选项目收集 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#php-sqlite"><span class="nav-number">1.</span> <span class="nav-text">php sqlite</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#S-O-I-L-D-之单一职责"><span class="nav-number">2.</span> <span class="nav-text">S.O.I.L.D 之单一职责</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#与微博内容分析相关的正则表达式"><span class="nav-number">3.</span> <span class="nav-text">与微博内容分析相关的正则表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Warning-count-Parameter-must-be-an-array-or-an-object"><span class="nav-number">4.</span> <span class="nav-text">Warning: count(): Parameter must be an array or an object</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无限级分类"><span class="nav-number">5.</span> <span class="nav-text">无限级分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#本周一的时间戳"><span class="nav-number">6.</span> <span class="nav-text">本周一的时间戳</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#广度优先搜索"><span class="nav-number">7.</span> <span class="nav-text">广度优先搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#归并排序"><span class="nav-number">8.</span> <span class="nav-text">归并排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#正则表达式实现-某人"><span class="nav-number">9.</span> <span class="nav-text">正则表达式实现 @某人</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#快排和归并排序"><span class="nav-number">10.</span> <span class="nav-text">快排和归并排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Jwt前后端分离"><span class="nav-number">11.</span> <span class="nav-text">Jwt前后端分离</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#递归"><span class="nav-number">12.</span> <span class="nav-text">递归</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel中chunk方法分块处理数据的坑"><span class="nav-number">13.</span> <span class="nav-text">Laravel中chunk方法分块处理数据的坑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代码细节"><span class="nav-number">14.</span> <span class="nav-text">代码细节</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-7-1-中使用-openssl-取代-mcrypt"><span class="nav-number">15.</span> <span class="nav-text">PHP 7.1 中使用 openssl 取代 mcrypt</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#static-效率优化"><span class="nav-number">16.</span> <span class="nav-text">static 效率优化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解码-JSONP"><span class="nav-number">17.</span> <span class="nav-text">解码 JSONP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#合并多维数组中的子数组"><span class="nav-number">18.</span> <span class="nav-text">合并多维数组中的子数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#simplexml-load-string-的-parser-error-问题"><span class="nav-number">19.</span> <span class="nav-text">simplexml_load_string 的 parser error 问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何移除控制字符"><span class="nav-number">20.</span> <span class="nav-text">如何移除控制字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取-Linux-服务器的-uptime"><span class="nav-number">21.</span> <span class="nav-text">获取 Linux 服务器的 uptime</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用Memcahced-的时候，请不要把过期时间设置成超过30天"><span class="nav-number">22.</span> <span class="nav-text">用Memcahced 的时候，请不要把过期时间设置成超过30天</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json-decode-无法解析"><span class="nav-number">23.</span> <span class="nav-text">json_decode 无法解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cURL-报错-error-60-SSL-certificate-problem"><span class="nav-number">24.</span> <span class="nav-text">cURL 报错 error 60 SSL certificate problem</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#按周取时间段"><span class="nav-number">25.</span> <span class="nav-text">按周取时间段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-代码安全"><span class="nav-number">26.</span> <span class="nav-text">PHP 代码安全</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名函数"><span class="nav-number">27.</span> <span class="nav-text">匿名函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#比较字符串"><span class="nav-number">28.</span> <span class="nav-text">比较字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#md5"><span class="nav-number">29.</span> <span class="nav-text">md5</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#方法多次调用"><span class="nav-number">30.</span> <span class="nav-text">方法多次调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#越权漏洞"><span class="nav-number">31.</span> <span class="nav-text">越权漏洞</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#限制分页条目"><span class="nav-number">32.</span> <span class="nav-text">限制分页条目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#XSS"><span class="nav-number">33.</span> <span class="nav-text">XSS</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CSRF"><span class="nav-number">34.</span> <span class="nav-text">CSRF</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#No-input-file-specified"><span class="nav-number">35.</span> <span class="nav-text">No input file specified</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一个简单的composer包"><span class="nav-number">36.</span> <span class="nav-text">一个简单的composer包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#jwt记录"><span class="nav-number">37.</span> <span class="nav-text">jwt记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-FPM-创建慢日志"><span class="nav-number">38.</span> <span class="nav-text">PHP-FPM 创建慢日志</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#正则表达式之难点"><span class="nav-number">39.</span> <span class="nav-text">正则表达式之难点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#S-O-I-L-D-之接口隔离"><span class="nav-number">40.</span> <span class="nav-text">S.O.I.L.D 之接口隔离</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#array-map"><span class="nav-number">41.</span> <span class="nav-text">array_map</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#intval-与-int"><span class="nav-number">42.</span> <span class="nav-text">intval() 与 (int)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json-encode-序列化非公开属性"><span class="nav-number">43.</span> <span class="nav-text">json_encode() 序列化非公开属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-引用详解"><span class="nav-number">44.</span> <span class="nav-text">PHP 引用详解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#封装代码"><span class="nav-number">45.</span> <span class="nav-text">封装代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无限极分类树"><span class="nav-number">46.</span> <span class="nav-text">无限极分类树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代理与反向代理、负载均衡和缓存"><span class="nav-number">47.</span> <span class="nav-text">代理与反向代理、负载均衡和缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Swoole-Redis-list实现简单消息推送"><span class="nav-number">48.</span> <span class="nav-text">Swoole,Redis list实现简单消息推送</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-list队列异常崩溃"><span class="nav-number">49.</span> <span class="nav-text">Redis list队列异常崩溃</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#从0-n之间取k个不重复的数"><span class="nav-number">50.</span> <span class="nav-text">从0-n之间取k个不重复的数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名函数-1"><span class="nav-number">51.</span> <span class="nav-text">匿名函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组整合合并"><span class="nav-number">52.</span> <span class="nav-text">数组整合合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#循环中查询数据库"><span class="nav-number">53.</span> <span class="nav-text">循环中查询数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json排序"><span class="nav-number">54.</span> <span class="nav-text">json排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组排序"><span class="nav-number">55.</span> <span class="nav-text">数组排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图片上传添加自动裁剪"><span class="nav-number">56.</span> <span class="nav-text">图片上传添加自动裁剪</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#快速接入-GitHub-登陆"><span class="nav-number">57.</span> <span class="nav-text">快速接入 GitHub 登陆</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-安全问题入门"><span class="nav-number">58.</span> <span class="nav-text">PHP 安全问题入门</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#内置-Web-服务器"><span class="nav-number">59.</span> <span class="nav-text">内置 Web 服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#phpexcel"><span class="nav-number">60.</span> <span class="nav-text">phpexcel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-多进程"><span class="nav-number">61.</span> <span class="nav-text">PHP 多进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-进程"><span class="nav-number">62.</span> <span class="nav-text">php 进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#码模拟实现队列"><span class="nav-number">63.</span> <span class="nav-text">码模拟实现队列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#夏令时"><span class="nav-number">64.</span> <span class="nav-text">夏令时</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#去除emoji"><span class="nav-number">65.</span> <span class="nav-text">去除emoji</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#imagick等比缩放图片"><span class="nav-number">66.</span> <span class="nav-text">imagick等比缩放图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#redis支持php的序列化和反序列化"><span class="nav-number">67.</span> <span class="nav-text">redis支持php的序列化和反序列化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一致性hash"><span class="nav-number">68.</span> <span class="nav-text">一致性hash</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#抽奖的经典算法"><span class="nav-number">69.</span> <span class="nav-text">抽奖的经典算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#本月第一天"><span class="nav-number">70.</span> <span class="nav-text">本月第一天</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多维数组转一维数组"><span class="nav-number">71.</span> <span class="nav-text">多维数组转一维数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#面向对象的三大特性"><span class="nav-number">72.</span> <span class="nav-text">面向对象的三大特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-并发"><span class="nav-number">73.</span> <span class="nav-text">PHP 并发</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-守护程序"><span class="nav-number">74.</span> <span class="nav-text">PHP 守护程序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSON-encode-精度缺失"><span class="nav-number">75.</span> <span class="nav-text">JSON_encode () 精度缺失</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#生成器"><span class="nav-number">76.</span> <span class="nav-text">生成器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串分割"><span class="nav-number">77.</span> <span class="nav-text">字符串分割</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#闭包和匿名函数"><span class="nav-number">78.</span> <span class="nav-text">闭包和匿名函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl绑定host"><span class="nav-number">79.</span> <span class="nav-text">curl绑定host</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#错误和异常"><span class="nav-number">80.</span> <span class="nav-text">错误和异常</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#装饰器"><span class="nav-number">81.</span> <span class="nav-text">装饰器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#理解-cookie、session、token"><span class="nav-number">82.</span> <span class="nav-text">理解 cookie、session、token</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分割字符串"><span class="nav-number">83.</span> <span class="nav-text">分割字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#计算身份证的最后一位验证码"><span class="nav-number">84.</span> <span class="nav-text">计算身份证的最后一位验证码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#简单工厂模式"><span class="nav-number">85.</span> <span class="nav-text">简单工厂模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#array-reduce-多值化一"><span class="nav-number">86.</span> <span class="nav-text">array_reduce 多值化一</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#过滤空白字符"><span class="nav-number">87.</span> <span class="nav-text">过滤空白字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl-上传图片"><span class="nav-number">88.</span> <span class="nav-text">curl 上传图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#快手短视频无水印"><span class="nav-number">89.</span> <span class="nav-text">快手短视频无水印</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#根据另外一个数组去重"><span class="nav-number">90.</span> <span class="nav-text">根据另外一个数组去重</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取字符串内的链接"><span class="nav-number">91.</span> <span class="nav-text">获取字符串内的链接</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
